1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "number_format.h"
17 #include "number_format_impl.h"
18
19 using namespace OHOS::I18N;
20
NumberFormat(LocaleInfo & locale,int & status)21 NumberFormat::NumberFormat(LocaleInfo &locale, int &status)
22 {
23 if (locale.GetId() == nullptr) {
24 status = IERROR;
25 return;
26 }
27 mLocale = locale;
28 }
29
Init()30 bool NumberFormat::Init()
31 {
32 int status = I18nStatus::ISUCCESS;
33 if (impl != nullptr) {
34 delete impl;
35 }
36 impl = new NumberFormatImpl(mLocale, status);
37 if (impl == nullptr) {
38 return false;
39 }
40 if (status != I18nStatus::ISUCCESS) {
41 return false;
42 }
43 DataResource resource(&mLocale);
44 bool isSuccess = resource.Init();
45 if (!isSuccess) {
46 return false;
47 }
48 return impl->Init(resource);
49 }
50
51
~NumberFormat()52 NumberFormat::~NumberFormat()
53 {
54 if (impl != nullptr) {
55 delete impl;
56 impl = nullptr;
57 }
58 }
59
60
Format(double num,NumberFormatType type,int & status)61 std::string NumberFormat::Format(double num, NumberFormatType type, int &status)
62 {
63 if (!ReInitImpl()) {
64 return "";
65 }
66 return impl->Format(num, type, status);
67 }
68
Format(int num,int & status)69 std::string NumberFormat::Format(int num, int &status)
70 {
71 if (!ReInitImpl()) {
72 return "";
73 }
74 return impl->Format(num, status);
75 }
76
FormatNoGroup(double num,NumberFormatType type,int & status)77 std::string NumberFormat::FormatNoGroup(double num, NumberFormatType type, int &status)
78 {
79 if (!ReInitImpl()) {
80 return "";
81 }
82 return impl->FormatNoGroup(num, type, status);
83 }
84
FormatNoGroup(int num,int & status)85 std::string NumberFormat::FormatNoGroup(int num, int &status)
86 {
87 if (!ReInitImpl()) {
88 return "";
89 }
90 return impl->FormatNoGroup(num, status);
91 }
92
SetMaxDecimalLength(int length)93 bool NumberFormat::SetMaxDecimalLength(int length)
94 {
95 if (!ReInitImpl()) {
96 return false;
97 }
98 return impl->SetMaxDecimalLength(length);
99 }
100
SetMinDecimalLength(int length)101 bool NumberFormat::SetMinDecimalLength(int length)
102 {
103 if (!ReInitImpl()) {
104 return false;
105 }
106 return impl->SetMinDecimalLength(length);
107 }
108
ReInitImpl()109 bool NumberFormat::ReInitImpl()
110 {
111 if (impl == nullptr) {
112 bool isSuccess = Init();
113 if (!isSuccess) {
114 return false;
115 }
116 }
117 return true;
118 }