1# 数字与度量衡国际化
2
3## 使用场景
4
5在不同的国家和文化中,数字、货币和度量衡的表示方法有所不同,包括什么符号作为小数分隔符、分隔符后显示几位数字、使用什么样的货币和度量衡单位等。例如,开发者需要在应用界面显示数字“1,000”(一千),用于表示一件商品的价格。若采用固定格式“1,000”,由于在欧洲某些国家(如德国)使用逗号表示小数点,用户会理解为“1”。为了使界面呈现格式符合当地人的使用习惯,需要对数字、货币和度量衡进行格式化,格式化后会根据用户当前设置的语言和地区进行显示。
6
7## 开发步骤
8
9### 数字格式化
10
11数字格式化通过[NumberFormat](../reference/apis-localization-kit/js-apis-intl.md#numberformat)的[format](../reference/apis-localization-kit/js-apis-intl.md#format-1)接口实现,具体开发步骤如下。
12
131. 导入模块。
14   ```ts
15   import { intl } from '@kit.LocalizationKit';
16   ```
17
182. 创建NumberFormat对象。
19   传入locale列表时,使用第一个有效的locale创建对象。不传入locale参数时,使用系统当前的locale创建对象。
20   构造函数支持通过NumberOptions设置不同的数字格式化格式,具体请参考表1-表8。
21
22   ```ts
23   let numberFormat: intl.NumberFormat = new intl.NumberFormat(locale: string | Array<string>, options?: NumberOptions);
24   ```
25
263. 数字格式化,根据numberFormat的设置格式化number。
27   ```ts
28   let formattedNumber: string = numberFormat.format(number: number);
29   ```
30
314. 获取数字格式化选项,查看对象的设置信息。
32   ```ts
33   let options: intl.NumberOptions = numberFormat.resolvedOptions();
34   ```
35
36**数字格式化选项**
37
38对于数字,通过[NumberOptions](../reference/apis-localization-kit/js-apis-intl.md#numberoptions)参数可以设置最小整数位数、最小小数位数、最大小数位数、最低有效位数、最大有效位数、是否分组显示、数字的格式化规格、紧凑型的显示格式,以及数字的显示格式和数字系统。其中,数字的显示格式包括decimal(十进制)、percent(百分数)、currency(货币)、unit(单位)。
39
40以123000.123为例,各属性参数取值和显示效果如下表所示。
41
42**表1** 最小整数位数(minimumIntegerDigits)
43
44| 取值 | 显示效果 |
45| -------- | -------- |
46| 6 | 123,000.123 |
47| 7 | 0,123,000.123 |
48
49**表2** 最小小数位数(minimumFractionDigits)
50
51| 取值 | 显示效果 |
52| -------- | -------- |
53| 3 | 123,000.123 |
54| 4 | 123,000.1230 |
55
56**表3** 最大小数位数(maximumFractionDigits)
57
58| 取值 | 显示效果 |
59| -------- | -------- |
60| 3 | 123,000.123 |
61| 2 | 123,000.12 |
62
63**表4** 最低有效位数(minimumSignificantDigits)
64
65| 取值 | 显示效果 |
66| -------- | -------- |
67| 9 | 123,000.123 |
68| 10 | 123,000.1230 |
69
70**表5** 最大有效位数(maximumSignificantDigits)
71
72| 取值 | 显示效果 |
73| -------- | -------- |
74| 9 | 123,000.123 |
75| 8 | 123,000.12 |
76
77**表6** 是否分组显示(useGrouping)
78
79| 取值 | 显示效果 |
80| -------- | -------- |
81| true | 123,000.123 |
82| false | 123000.123 |
83
84**表7** 数字的格式化规格(notation)
85
86| 取值 | 显示效果 |
87| -------- | -------- |
88| standard | 123,000.123 |
89| scientific | 1.230001E5 |
90| engineering | 123.000123E3 |
91| compact | 123K |
92
93**表8** 紧凑型的显示格式(compactDisplay)
94
95| 取值 | 显示效果 |
96| -------- | -------- |
97| short | 123K |
98| long | 123 thousand |
99
100
101**开发实例**
102
103```ts
104// 导入模块
105import { intl } from '@kit.LocalizationKit';
106
107// 用科学计数法显示数字
108let numberFormat1 = new intl.NumberFormat('zh-CN', {notation: 'scientific', maximumSignificantDigits: 3});
109let formattedNumber1 = numberFormat1.format(123400); // formattedNumber1: 1.23E5
110
111// 用紧凑的格式显示数字
112let numberFormat2 = new intl.NumberFormat('zh-CN', {notation: 'compact', compactDisplay: 'short'});
113let formattedNumber2 = numberFormat2.format(123400); // formattedNumber2: 12万
114
115// 显示数字的符号
116let numberFormat3 = new intl.NumberFormat('zh-CN', {signDisplay: 'always'});
117let formattedNumber3 = numberFormat3.format(123400); // formattedNumber3: +123,400
118
119// 显示百分数
120let numberFormat4 = new intl.NumberFormat('zh-CN', {style: 'percent'});
121let formattedNumber4 = numberFormat4.format(0.25); // formattedNumber4: 25%
122```
123
124
125### 货币和单位格式化
126
127货币和单位的格式化基于数字格式化,在创建货币和单元格式化对象时,将数字的显示风格分别设置为“currency(货币)”和“unit(单位)”。同样,货币和单位的构造函数也支持通过[NumberOptions](../reference/apis-localization-kit/js-apis-intl.md#numberoptions)设置不同的格式,各属性参数取值和显示效果如下表所示。
128
129**货币格式化选项**
130
131以货币单位: USD,数值: -12300为例。
132
133**表9** 货币单位的符号(currencySign)
134
135| 取值 | 显示效果 |
136| -------- | -------- |
137| standard | -US$12,300.00 |
138| accounting | (US$12,300.00) |
139
140**表10** 货币的显示方式(currencyDisplay)
141
142| 取值 | 显示效果 |
143| -------- | -------- |
144| symbol | -US$12,300.00 |
145| narrowSymbol | -$12,300.00 |
146| code | -USD 12,300.00 |
147| name | -12,300.00 US dollars |
148
149**单位格式化选项**
150
151以单位名称:hectare,数字大小:-12300为例。
152
153**表11** 单位的显示格式(unitDisplay)
154
155| 取值 | 显示效果 |
156| -------- | -------- |
157| long | -12,3000 hectares |
158| short | -12,300 ha |
159| narrow | -12,300ha |
160
161**表12** 单位的使用场景(unitUsage)
162
163| 取值 | 显示效果 |
164| -------- | -------- |
165| 不设置 | -12,300 ha |
166| default | -47.491 sq mi |
167| area-land-agricult | -30,393.962 ac |
168
169
170**开发实例**
171```ts
172// 导入模块
173import { intl } from '@kit.LocalizationKit';
174
175// 格式化货币
176let numberFormat5 = new intl.NumberFormat('zh-CN', {style: 'currency', currency: 'USD'});
177let formattedNumber5 = numberFormat5.format(123400); // formattedNumber5: US$123,400.00
178
179// 用名称表示货币
180let numberFormat6 = new intl.NumberFormat('zh-CN', {style: 'currency', currency: 'USD', currencyDisplay: 'name'});
181let formattedNumber6 = numberFormat6.format(123400); // formattedNumber6: 123,400.00美元
182
183// 格式化度量衡
184let numberFormat7 = new intl.NumberFormat('en-GB', {style: 'unit', unit: 'hectare'});
185let formattedNumber7 = numberFormat7.format(123400); // formattedNumber7: 123,400 ha
186
187// 格式化特定场景下度量衡,如面积-土地-农业
188let numberFormat8 = new intl.NumberFormat('en-GB', {style: 'unit', unit: 'hectare', unitUsage: 'area-land-agricult'});
189let formattedNumber8 = numberFormat8.format(123400); // formattedNumber8: 304,928.041 ac
190```
191
192
193### 度量衡转换
194
195单位转换并根据区域和风格进行格式化,通过[I18NUtil](../reference/apis-localization-kit/js-apis-i18n.md#i18nutil9)类的[unitConvert](../reference/apis-localization-kit/js-apis-i18n.md#unitconvert9)接口实现,具体开发步骤如下。
196
1971. 导入模块。
198   ```ts
199   import { i18n } from '@kit.LocalizationKit';
200   ```
201
2022. 度量衡转换。
203
204   将度量衡从fromUnit转换到toUnit,数值为value,并根据区域和风格进行格式化。style可取不同的值,显示不用效果,具体请参考表13。
205   ```ts
206   let convertedUnit: string = i18n.I18NUtil.unitConvert(fromUnit: UnitInfo, toUnit: UnitInfo, value: number, locale: string, style?: string);
207   ```
208
209**格式化风格**
210
211以fromUnit为美制单位cup,toUnit为公制单位liter,数字大小:1000为例。
212
213**表13** 格式化使用的风格(style)
214
215| 取值 | 显示效果 |
216| -------- | -------- |
217| long | 236.588 liters |
218| short | 236.588 L |
219| narrow | 236.588L |
220
221**开发实例**
222
223```ts
224// 导入模块
225import { i18n } from '@kit.LocalizationKit';
226
227// 设置要转换的单位和目标单位
228let fromUnit: i18n.UnitInfo = {unit: 'cup', measureSystem: 'US'};
229let toUnit: i18n.UnitInfo = {unit: 'liter', measureSystem: 'SI'};
230
231// 以en-US区域参数转换度量衡
232let convertedUnit1 = i18n.I18NUtil.unitConvert(fromUnit, toUnit, 1000, 'en-US'); // convertedUnit1: 236.588 L
233
234// 显示完整的度量衡
235let convertedUnit2 = i18n.I18NUtil.unitConvert(fromUnit, toUnit, 1000, 'en-US', 'long'); // convertedUnit2: 236.588 liters
236```
237