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 #ifndef NUMBERFORMAT_H 17 #define NUMBERFORMAT_H 18 19 /** 20 * @addtogroup I18N 21 * @{ 22 * 23 * @brief Provides functions related to internationalization (i18n), with which you can format date, time and numbers. 24 * 25 * @since 2.2 26 * @version 1.0 27 */ 28 29 /** 30 * @file number_format.h 31 * 32 * @brief Declares functions for formatting integers and double numbers. 33 * 34 * Example code: \n 35 * Creating a <b>LocaleInfo</b> instance: \n 36 * {@code LocaleInfo locale("en", "US");} 37 * Creating a <b>NumberFormat</b> instance: \n 38 * {@code 39 * int status = 0; 40 * NumberFormat formatter(locale, status); 41 * Formatting data: \n 42 * {@code 43 * int num = 1234 44 * std::string out = formatter.Format(num, status);} 45 * Output: \n 46 * 1,234 47 * 48 * @since 2.2 49 * @version 1.0 50 */ 51 52 #include "types.h" 53 #include "securec.h" 54 #include "number_format_impl.h" 55 #include "locale_info.h" 56 57 namespace OHOS { 58 namespace I18N { 59 class NumberFormat { 60 public: 61 /** 62 * @brief A constructor used to create a <b>NumberFormat</b> instance with specified locale information. 63 * 64 * @param status Specifies whether a <b>NumberFormat</b> instance is created. 65 * The value <b>0</b> indicates that a <b>NumberFormat</b> instance is created, 66 * and the value <b>1</b> indicates the opposite case. 67 * @param locale Indicates the specified locale information. 68 * @since 2.2 69 * @version 1.0 70 */ 71 NumberFormat(LocaleInfo &locale, int &status); 72 73 /** 74 * @brief A destructor used to delete the <b>NumberFormat</b> instance. 75 * 76 * @since 2.2 77 * @version 1.0 78 */ 79 virtual ~NumberFormat(); 80 81 /** 82 * @brief Formats a double number. 83 * 84 * @param num Indicates the double number to format. 85 * @param type Indicates the type the double number is formatted into. 86 * The value can be <b>DECIMAL</b> or <b>PERCENT</b>. 87 * @param status Specifies whether the formatting is successful. 88 * The value <b>0</b> indicates that the formatting is successful, 89 * and <b>1</b> indicates that the formatting fails. 90 * @return Returns a string representation of the formatted double number. 91 * @since 2.2 92 * @version 1.0 93 */ 94 std::string Format(double num, NumberFormatType type, int &status); 95 96 /** 97 * @brief Formats an integer. 98 * 99 * @param num Indicates the integer to format. 100 * @param status Specifies whether the formatting is successful. 101 * The value <b>0</b> indicates that the formatting is successful, 102 * and <b>1</b> indicates that the formatting fails. 103 * @return Returns a string representation of the formatted integer. 104 * @since 2.2 105 * @version 1.0 106 */ 107 std::string Format(int num, int &status); 108 109 /** 110 * @brief Formats a double number without grouping its integer part. 111 * 112 * @param num Indicates the double number to format. 113 * @param type Indicates the type the double number is formatted into. 114 * The value can be <b>DECIMAL</b> or <b>PERCENT</b>. 115 * @param status Specifies whether the formatting is successful. The value <b>0</b> 116 * indicates that the formatting is successful, and <b>1</b> indicates that the formatting fails. 117 * @return Returns a string representation of the formatted double number. 118 * @since 2.2 119 * @version 1.0 120 */ 121 std::string FormatNoGroup(double num, NumberFormatType type, int &status); 122 123 /** 124 * @brief Formats an integer without grouping. 125 * 126 * @param num Indicates the integer to format. 127 * @param status Specifies whether the formatting is successful. The value <b>0</b> indicates that 128 * the formatting is successful, and <b>1</b> indicates that the formatting fails. 129 * @return Returns a string representation of the formatted double integer. 130 * @since 2.2 131 * @version 1.0 132 */ 133 std::string FormatNoGroup(int num, int &status); 134 135 /** 136 * @brief Performs an initialization to load data. 137 * 138 * @return Returns <b>true</b> if the initialization is successful; returns <b>false</b> otherwise. 139 * @since 2.2 140 * @version 1.0 141 */ 142 bool Init(); 143 144 /** 145 * @brief Sets the maximum length for the decimal part of a double number. The excess part will be truncated. 146 * 147 * @param length Indicates the maximum length to set. 148 * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise. 149 * @since 2.2 150 * @version 1.0 151 */ 152 bool SetMaxDecimalLength(int length); 153 154 /** 155 * @brief Sets the minimum length for the decimal part of a double number. 156 * Zero padding is required if the minimum length is not reached. 157 * 158 * @param length Indicates the minimum length to set. 159 * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise. 160 * @since 2.2 161 * @version 1.0 162 */ 163 bool SetMinDecimalLength(int length); 164 private: 165 bool ReInitImpl(); 166 NumberFormatImpl *impl = nullptr; 167 LocaleInfo mLocale; 168 }; 169 } // namespace I18N 170 } // namespace OHOS 171 /** @} */ 172 #endif