1 /*
2 * Copyright (c) 2025 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 "format_utils.h"
17
18 namespace OHOS {
19 namespace Global {
20 namespace I18n {
21 const std::unordered_map<UNumberFormatFields, std::string> FormatUtils::NUMBER_FORMAT_FIELD_MAP = {
22 { UNUM_FRACTION_FIELD, "fraction" },
23 { UNUM_DECIMAL_SEPARATOR_FIELD, "decimal" },
24 { UNUM_GROUPING_SEPARATOR_FIELD, "group" },
25 { UNUM_CURRENCY_FIELD, "currency" },
26 { UNUM_PERCENT_FIELD, "percentSign" },
27 { UNUM_EXPONENT_SYMBOL_FIELD, "exponentSeparator" },
28 { UNUM_EXPONENT_SIGN_FIELD, "exponentMinusSign" },
29 { UNUM_EXPONENT_FIELD, "exponentInteger" },
30 { UNUM_COMPACT_FIELD, "compact" },
31 { UNUM_MEASURE_UNIT_FIELD, "unit" },
32 { UNUM_APPROXIMATELY_SIGN_FIELD, "approximatelySign" },
33 };
34
GetNumberFieldType(const std::string & napiType,const int32_t fieldId,const double number)35 std::string FormatUtils::GetNumberFieldType(const std::string &napiType,
36 const int32_t fieldId, const double number)
37 {
38 UNumberFormatFields formatField = static_cast<UNumberFormatFields>(fieldId);
39 if (NUMBER_FORMAT_FIELD_MAP.find(formatField) != NUMBER_FORMAT_FIELD_MAP.end()) {
40 return NUMBER_FORMAT_FIELD_MAP.at(formatField);
41 }
42 bool isBigint = napiType.compare("bigint") == 0;
43 switch (formatField) {
44 case UNUM_INTEGER_FIELD:
45 if (isBigint || std::isfinite(number)) {
46 return "integer";
47 }
48 if (std::isnan(number)) {
49 return "nan";
50 }
51 return "infinity";
52 case UNUM_SIGN_FIELD:
53 if (isBigint) {
54 return number < 0 ? "minusSign" : "plusSign";
55 }
56 return std::signbit(number) ? "minusSign" : "plusSign";
57 default:
58 return "unknown";
59 }
60 }
61 } // namespace I18n
62 } // namespace Global
63 } // namespace OHOS
64