• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.intl (国际化-Intl)
2
3本模块提供基础的应用国际化能力,包括时间日期格式化、数字格式化、排序等,相关接口在ECMA 402标准中定义。
4[I18N模块](js-apis-i18n.md)提供其他非ECMA 402定义的国际化接口,与本模块共同使用可提供完整地国际化支持能力。
5
6>  **说明:**
7>
8>  - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9>
10>  - 本模块接口依据[CLDR](https://cldr.unicode.org) 国际化数据库进行处理,随着CLDR演进,本模块接口处理结果可能发生变化。其中,API version 12对应[CLDR 42](https://cldr.unicode.org/index/downloads/cldr-42),详细数据变化请参考官方链接。
11>
12>  - 从API version 11开始,本模块部分接口支持在ArkTS卡片中使用。
13>
14>  - 从API version 12开始,本模块全接口支持在原子化服务中使用。
15
16
17## 导入模块
18
19```ts
20import { intl } from '@kit.LocalizationKit';
21```
22
23## Locale
24
25### 属性
26
27**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
28
29**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
30
31**系统能力**:SystemCapability.Global.I18n
32
33| 名称              | 类型      | 必填   | 说明                                       |
34| --------------- | ------- | -------- | ---------------------------------------- |
35| language        | string  | 是    | 与区域设置相关的语言,如:zh。取值遵循ISO 639标准。 |
36| script          | string  | 是    | 区域语言的书写方式(脚本),如:Hans。取值遵循Unicode ISO 15924标准。 |
37| region          | string  | 是    | 与区域设置相关的国家或地区,如:CN。取值遵循ISO 3166标准。 |
38| baseName        | string  | 是    | 区域对象的基本信息,由语言、脚本、国家或地区组成,如:zh-Hans-CN。  |
39| caseFirst       | string  | 是    | 区域的排序规则是否考虑大小写,取值包括:<br>"upper",&nbsp;"lower",&nbsp;"false"。<br>不同取值表示的含义请参考[本地习惯排序表1](../../internationalization/i18n-sorting-local.md)。 |
40| calendar        | string  | 是    | 区域的日历信息,取值包括:<br>"buddhist",&nbsp;"chinese",&nbsp;"coptic","dangi",&nbsp;"ethioaa",&nbsp;"ethiopic",&nbsp;"gregory",&nbsp;"hebrew",&nbsp;"indian",&nbsp;"islamic",&nbsp;"islamic-umalqura",&nbsp;"islamic-tbla",&nbsp;"islamic-civil",&nbsp;"islamic-rgsa",&nbsp;"iso8601",&nbsp;"japanese",&nbsp;"persian",&nbsp;"roc",&nbsp;"islamicc"。<br>不同取值表示的含义请参考[设置日历和历法表1](../../internationalization/i18n-calendar.md)。 |
41| collation       | string  | 是    | 区域的排序规则,取值包括:<br>"big5han",&nbsp;"compat",&nbsp;"dict",&nbsp;"direct",&nbsp;"ducet",&nbsp;"eor",&nbsp;"gb2312",&nbsp;"phonebk",&nbsp;"phonetic",&nbsp;"pinyin",&nbsp;"reformed",&nbsp;"searchjl",&nbsp;"stroke",&nbsp;"trad",&nbsp;"unihan",&nbsp;"zhuyin"。<br>不同取值表示的含义请参考[本地习惯排序表1](../../internationalization/i18n-sorting-local.md)。 |
42| hourCycle       | string  | 是    | 区域的时制信息,取值包括:<br>"h11"、"h12"、"h23"、"h24"。<br>不同取值的显示效果可参考[时间日期国际化表5](../../internationalization/i18n-time-date.md)。 |
43| numberingSystem | string  | 是    | 区域使用的数字系统,取值包括:<br>"adlm",&nbsp;"ahom",&nbsp;"arab",&nbsp;"arabext",&nbsp;"bali",&nbsp;"beng",&nbsp;"bhks",&nbsp;"brah",&nbsp;"cakm",&nbsp;"cham",&nbsp;"deva",&nbsp;"diak",&nbsp;"fullwide",&nbsp;"gong",&nbsp;"gonm",&nbsp;"gujr",&nbsp;"guru",&nbsp;"hanidec",&nbsp;"hmng",&nbsp;"hmnp",&nbsp;"java",&nbsp;"kali",&nbsp;"khmr",&nbsp;"knda",&nbsp;"lana",&nbsp;"lanatham",&nbsp;"laoo",&nbsp;"latn",&nbsp;"lepc",&nbsp;"limb",&nbsp;"mathbold",&nbsp;"mathdbl",&nbsp;"mathmono",&nbsp;"mathsanb",&nbsp;"mathsans",&nbsp;"mlym",&nbsp;"modi",&nbsp;"mong",&nbsp;"mroo",&nbsp;"mtei",&nbsp;"mymr",&nbsp;"mymrshan",&nbsp;"mymrtlng",&nbsp;"newa",&nbsp;"nkoo",&nbsp;"olck",&nbsp;"orya",&nbsp;"osma",&nbsp;"rohg",&nbsp;"saur",&nbsp;"segment",&nbsp;"shrd",&nbsp;"sind",&nbsp;"sinh",&nbsp;"sora",&nbsp;"sund",&nbsp;"takr",&nbsp;"talu",&nbsp;"tamldec",&nbsp;"telu",&nbsp;"thai",&nbsp;"tibt",&nbsp;"tirh",&nbsp;"vaii",&nbsp;"wara",&nbsp;"wcho"。 |
44| numeric         | boolean | 是    | true表示对数字字符进行特殊的排序规则处理,false表示不对数字字符进行特殊的排序规则处理。<br>默认值:false。                      |
45
46### constructor<sup>8+</sup>
47
48constructor()
49
50创建区域对象。
51
52**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
53
54**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
55
56**系统能力**:SystemCapability.Global.I18n
57
58**示例:**
59  ```ts
60  // 默认构造函数使用系统当前区域ID创建
61  let locale = new intl.Locale();
62  // 返回系统当前区域ID
63  let localeID = locale.toString();
64  ```
65
66### constructor
67
68constructor(locale: string, options?: LocaleOptions)
69
70创建区域对象。
71
72**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
73
74**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
75
76**系统能力**:SystemCapability.Global.I18n
77
78**参数:**
79
80| 参数名                  | 类型                               | 必填   | 说明                           |
81| -------------------- | -------------------------------- | ---- | ---------------------------- |
82| locale               | string                           | 是    | 表示区域ID的字符串,由语言、脚本、国家或地区组成。<br>区域ID可填写组成部分中的一个或多个。|
83| options             | [LocaleOptions](#localeoptions) | 否    | 创建区域对象的选项。 |
84
85**示例:**
86  ```ts
87  // 创建zh-CN区域对象
88  let locale = new intl.Locale("zh-CN");
89  let localeID = locale.toString(); // localeID = "zh-CN"
90  ```
91
92
93### toString
94
95toString(): string
96
97获取区域对象的字符串。
98
99**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
100
101**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
102
103**系统能力**:SystemCapability.Global.I18n
104
105**返回值:**
106
107| 类型     | 说明          |
108| ------ | ----------- |
109| string | 区域对象的字符串。 |
110
111**示例:**
112  ```ts
113  // 创建en-GB区域对象
114  let locale = new intl.Locale("en-GB");
115  let localeID = locale.toString(); // localeID = "en-GB"
116  ```
117
118### maximize
119
120maximize(): Locale
121
122最大化区域信息,可补齐区域对象中缺少脚本、国家或地区信息。
123
124**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
125
126**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
127
128**系统能力**:SystemCapability.Global.I18n
129
130**返回值:**
131
132| 类型                | 说明         |
133| ----------------- | ---------- |
134| [Locale](#locale) | 补充完脚本、国家或地区信息后的区域对象。 |
135
136**示例:**
137  ```ts
138  // 创建zh区域对象
139  let locale = new intl.Locale("zh");
140  // 补齐区域对象的脚本和地区
141  let maximizedLocale = locale.maximize();
142  let localeID = maximizedLocale.toString(); // localeID = "zh-Hans-CN"
143
144  // 创建en-US区域对象
145  locale = new intl.Locale("en-US");
146  // 补齐区域对象的脚本
147  maximizedLocale = locale.maximize();
148  localeID = maximizedLocale.toString(); // localeID = "en-Latn-US"
149  ```
150
151
152### minimize
153
154minimize(): Locale
155
156最小化区域信息,可删除区域对象中的脚本、国家或地区信息。
157
158**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
159
160**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
161
162**系统能力**:SystemCapability.Global.I18n
163
164**返回值:**
165
166| 类型                | 说明         |
167| ----------------- | ---------- |
168| [Locale](#locale) | 删除完脚本、国家或地区信息后的区域对象。 |
169
170**示例:**
171  ```ts
172  // 创建zh-Hans-CN区域对象
173  let locale = new intl.Locale("zh-Hans-CN");
174  // 去除区域对象的脚本和地区
175  let minimizedLocale = locale.minimize();
176  let localeID = minimizedLocale.toString(); // localeID = "zh"
177
178  // 创建en-US区域对象
179  locale = new intl.Locale("en-US");
180  // 去除区域对象的地区
181  minimizedLocale = locale.minimize();
182  localeID = minimizedLocale.toString(); // localeID = "en"
183  ```
184
185## LocaleOptions
186
187区域初始化选项。从API9开始,LocaleOptions属性由必填改为可选。
188
189**卡片能力**:从API version 11开始,该类型支持在ArkTS卡片中使用。
190
191**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
192
193**系统能力**:SystemCapability.Global.I18n
194
195| 名称              | 类型      | 必填   |  说明                                       |
196| --------------- | ------- | ---- |---------------------------------------- |
197| calendar        | string  | 否   |日历参数,取值包括:<br>"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc"。 |
198| collation       | string  | 否     |排序参数,取值包括:<br>"big5han", "compat", "dict", "direct", "ducet", "emoji", "eor", "gb2312", "phonebk", "phonetic", "pinyin", "reformed	", "search", "searchjl", "standard", "stroke", "trad", "unihan", "zhuyin"。 |
199| hourCycle       | string  | 否     |时制格式,取值包括:<br>"h11",&nbsp;"h12",&nbsp;"h23",&nbsp;"h24"。 |
200| numberingSystem | string  | 否     |数字系统,取值包括:<br>"adlm",&nbsp;"ahom",&nbsp;"arab",&nbsp;"arabext",&nbsp;"bali",&nbsp;"beng",&nbsp;"bhks",&nbsp;"brah",&nbsp;"cakm",&nbsp;"cham",&nbsp;"deva",&nbsp;"diak",&nbsp;"fullwide",&nbsp;"gong",&nbsp;"gonm",&nbsp;"gujr",&nbsp;"guru",&nbsp;"hanidec",&nbsp;"hmng",&nbsp;"hmnp",&nbsp;"java",&nbsp;"kali",&nbsp;"khmr",&nbsp;"knda",&nbsp;"lana",&nbsp;"lanatham",&nbsp;"laoo",&nbsp;"latn",&nbsp;"lepc",&nbsp;"limb",&nbsp;"mathbold",&nbsp;"mathdbl",&nbsp;"mathmono",&nbsp;"mathsanb",&nbsp;"mathsans",&nbsp;"mlym",&nbsp;"modi",&nbsp;"mong",&nbsp;"mroo",&nbsp;"mtei",&nbsp;"mymr",&nbsp;"mymrshan",&nbsp;"mymrtlng",&nbsp;"newa",&nbsp;"nkoo",&nbsp;"olck",&nbsp;"orya",&nbsp;"osma",&nbsp;"rohg",&nbsp;"saur",&nbsp;"segment",&nbsp;"shrd",&nbsp;"sind",&nbsp;"sinh",&nbsp;"sora",&nbsp;"sund",&nbsp;"takr",&nbsp;"talu",&nbsp;"tamldec",&nbsp;"telu",&nbsp;"thai",&nbsp;"tibt",&nbsp;"tirh",&nbsp;"vaii",&nbsp;"wara",&nbsp;"wcho"。 |
201| numeric         | boolean | 否     | true表示对数字字符进行特殊的排序规则处理,false表示不对数字字符进行特殊的排序规则处理。默认值:false。                               |
202| caseFirst       | string  | 否     | 表示大写、小写的排序顺序,取值范围:<br>"upper",&nbsp;"lower",&nbsp;"false"。 |
203
204>  **说明:**
205>
206>  - calendar:不同取值表示的含义请参考[设置日历和历法表1](../../internationalization/i18n-calendar.md)。
207>
208>  - hourCycle:不同取值的显示效果可参考[时间日期国际化表5](../../internationalization/i18n-time-date.md)。
209>
210>  - collation、caseFirst:不同取值表示的含义请参考[本地习惯排序表1](../../internationalization/i18n-sorting-local.md)。
211
212## DateTimeFormat
213
214### constructor<sup>8+</sup>
215
216constructor()
217
218创建时间、日期格式化对象。
219
220**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
221
222**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
223
224**系统能力**:SystemCapability.Global.I18n
225
226**示例:**
227  ```ts
228  // 使用系统当前区域ID创建DateTimeFormat对象
229  let datefmt= new intl.DateTimeFormat();
230  ```
231
232### constructor
233
234constructor(locale: string | Array&lt;string&gt;, options?: DateTimeOptions)
235
236创建时间、日期格式化对象。
237
238**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
239
240**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
241
242**系统能力**:SystemCapability.Global.I18n
243
244**参数:**
245
246| 参数名                  | 类型                                   | 必填   | 说明                           |
247| -------------------- | ------------------------------------ | ---- | ---------------------------- |
248| locale               | string \| Array&lt;string&gt;        | 是    | 表示区域ID的字符串,由语言、脚本、国家或地区组成。<br>区域ID可填写组成部分中的一个或多个。 |
249| options              | [DateTimeOptions](#datetimeoptions) | 否    | 创建时间、日期格式化对象时可设置的配置项。<br>若所有选项均未设置时,year、month、day三个属性的默认值为numeric。 |
250
251**示例:**
252  ```ts
253  // 使用zh-CN区域ID创建DateTimeFormat对象,日期风格为full,时间风格为medium
254  let datefmt= new intl.DateTimeFormat("zh-CN", { dateStyle: 'full', timeStyle: 'medium' });
255
256  // 使用区域ID列表创建DateTimeFormat对象,因为ban为非法区域ID,因此使用zh区域ID创建DateTimeFormat对象
257  let datefmt= new intl.DateTimeFormat(["ban", "zh"], { dateStyle: 'full', timeStyle: 'medium' });
258  ```
259
260### format
261
262format(date: Date): string
263
264对时间、日期进行格式化。
265
266**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
267
268**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
269
270**系统能力**:SystemCapability.Global.I18n
271
272**参数:**
273
274| 参数名  | 类型   | 必填   | 说明      |
275| ---- | ---- | ---- | ------- |
276| date | Date | 是    | 时间、日期。说明:月份从0开始计数,如0表示一月。 |
277
278**返回值:**
279
280| 类型     | 说明           |
281| ------ | ------------ |
282| string | 格式化后的时间、日期字符串。 |
283
284**示例:**
285  ```ts
286  let date = new Date(2021, 11, 17, 3, 24, 0); // 时间日期为2021.12.17 03:24:00
287  // 使用en-GB区域ID创建DateTimeFormat对象
288  let datefmt = new intl.DateTimeFormat("en-GB");
289  let formattedDate = datefmt.format(date); // formattedDate "17/12/2021"
290
291  // 使用en-GB区域ID创建DateTimeFormat对象,dateStyle设置为full,timeStyle设置为medium
292  datefmt = new intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
293  formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00"
294  ```
295
296### formatRange
297
298formatRange(startDate: Date, endDate: Date): string
299
300对时间段、日期段进行格式化。
301
302**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
303
304**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
305
306**系统能力**:SystemCapability.Global.I18n
307
308**参数:**
309
310| 参数名       | 类型   | 必填   | 说明       |
311| --------- | ---- | ---- | -------- |
312| startDate | Date | 是    | 时间、日期的开始。说明:月份从0开始计数,如0表示一月。|
313| endDate   | Date | 是    | 时间、日期的结束。说明:月份从0开始计数,如0表示一月。|
314
315**返回值:**
316
317| 类型     | 说明             |
318| ------ | -------------- |
319| string | 格式化后的时间段、日期段字符串。 |
320
321**示例:**
322  ```ts
323  let startDate = new Date(2021, 11, 17, 3, 24, 0); // 时间日期为2021.12.17 03:24:00
324  let endDate = new Date(2021, 11, 18, 3, 24, 0);
325  // 使用en-GB区域ID创建DateTimeFormat对象
326  let datefmt = new intl.DateTimeFormat("en-GB");
327  let formattedDateRange = datefmt.formatRange(startDate, endDate); // formattedDateRange = "17/12/2021 - 18/12/2021"
328  ```
329
330### resolvedOptions
331
332resolvedOptions(): DateTimeOptions
333
334获取创建时间、日期格式化对象时设置的配置项。
335
336**卡片能力**:从API version 11开始,该接口支持在ArkTS卡片中使用。
337
338**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
339
340**系统能力**:SystemCapability.Global.I18n
341
342**返回值:**
343
344| 类型                                   | 说明                            |
345| ------------------------------------ | ----------------------------- |
346| [DateTimeOptions](#datetimeoptions) | 时间、日期格式化对象设置的配置项。 |
347
348**示例:**
349  ```ts
350  let datefmt = new intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
351  // 返回DateTimeFormat对象的配置项
352  let options = datefmt.resolvedOptions();
353  let dateStyle = options.dateStyle; // dateStyle = "full"
354  let timeStyle = options.timeStyle; // timeStyle = "medium"
355  ```
356
357
358## DateTimeOptions
359
360时间、日期格式化时可设置的配置项。从API9开始,DateTimeOptions的属性由必填改为可选。
361
362**卡片能力**:从API version 11开始,该类型支持在ArkTS卡片中使用。
363
364**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
365
366**系统能力**:SystemCapability.Global.I18n
367
368| 名称              | 类型      | 必填   | 说明                                       |
369| --------------- | ------- | ---- |  ---------------------------------------- |
370| locale          | string  | 否    |合法的区域ID,&nbsp;如:zh-Hans-CN。<br>默认值:系统当前区域ID。           |
371| dateStyle       | string  | 否     |日期显示格式,取值包括:<br>"long",&nbsp;"short",&nbsp;"medium",&nbsp;"full",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表1](../../internationalization/i18n-time-date.md)。 |
372| timeStyle       | string  | 否     |时间显示格式,取值包括:<br>"long",&nbsp;"short",&nbsp;"medium",&nbsp;"full",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表2](../../internationalization/i18n-time-date.md)。 |
373| hourCycle       | string  | 否     |时制格式,取值包括:<br>"h11",&nbsp;"h12",&nbsp;"h23",&nbsp;"h24"。<br>不设置dateStyle或timeStyle参数时的显示效果请参考[时间日期国际化表5](../../internationalization/i18n-time-date.md)。<br>设置dateStyle或timeStyle参数时的显示效果请参考[时间日期国际化表6](../../internationalization/i18n-time-date.md)。 |
374| timeZone        | string  | 否     |使用的时区,取值为合法的IANA时区ID。                      |
375| numberingSystem | string  | 否     |数字系统,取值包括:<br>"adlm",&nbsp;"ahom",&nbsp;"arab",&nbsp;"arabext",&nbsp;"bali",&nbsp;"beng",&nbsp;"bhks",&nbsp;"brah",&nbsp;"cakm",&nbsp;"cham",&nbsp;"deva",&nbsp;"diak",&nbsp;"fullwide",&nbsp;"gong",&nbsp;"gonm",&nbsp;"gujr",&nbsp;"guru",&nbsp;"hanidec",&nbsp;"hmng",&nbsp;"hmnp",&nbsp;"java",&nbsp;"kali",&nbsp;"khmr",&nbsp;"knda",&nbsp;"lana",&nbsp;"lanatham",&nbsp;"laoo",&nbsp;"latn",&nbsp;"lepc",&nbsp;"limb",&nbsp;"mathbold",&nbsp;"mathdbl",&nbsp;"mathmono",&nbsp;"mathsanb",&nbsp;"mathsans",&nbsp;"mlym",&nbsp;"modi",&nbsp;"mong",&nbsp;"mroo",&nbsp;"mtei",&nbsp;"mymr",&nbsp;"mymrshan",&nbsp;"mymrtlng",&nbsp;"newa",&nbsp;"nkoo",&nbsp;"olck",&nbsp;"orya",&nbsp;"osma",&nbsp;"rohg",&nbsp;"saur",&nbsp;"segment",&nbsp;"shrd",&nbsp;"sind",&nbsp;"sinh",&nbsp;"sora",&nbsp;"sund",&nbsp;"takr",&nbsp;"talu",&nbsp;"tamldec",&nbsp;"telu",&nbsp;"thai",&nbsp;"tibt",&nbsp;"tirh",&nbsp;"vaii",&nbsp;"wara",&nbsp;"wcho"。 |
376| hour12          | boolean | 否     | true表示使用12小时制,false表示使用24小时制。<br>同时设置hour12和hourCycle时,hourCycle不生效。<br>若hour12和hourCycle未设置且系统24小时开关打开时,hour12属性的默认值为false。|
377| weekday         | string  | 否     | 星期的显示格式,取值包括:<br>"long",&nbsp;"short",&nbsp;"narrow",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表4](../../internationalization/i18n-time-date.md)。 |
378| era             | string  | 否     | 纪元的显示格式,取值包括:<br>"long",&nbsp;"short",&nbsp;"narrow",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表9](../../internationalization/i18n-time-date.md)。 |
379| year            | string  | 否     | 年份的显示格式,取值包括:<br>"numeric",&nbsp;"2-digit"。<br>不同取值的显示效果请参考[时间日期国际化表3](../../internationalization/i18n-time-date.md)。  |
380| month           | string  | 否    | 月份的显示格式,取值包括:<br>"numeric",&nbsp;"2-digit",&nbsp;"long",&nbsp;"short",&nbsp;"narrow",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表6](../../internationalization/i18n-time-date.md)。 |
381| day             | string  | 否     | 日期的显示格式,取值包括:<br>"numeric",&nbsp;"2-digit"。  |
382| hour            | string  | 否     | 小时的显示格式,取值包括:<br>"numeric",&nbsp;"2-digit"。  |
383| minute          | string  | 否     | 分钟的显示格式,取值包括:<br>"numeric",&nbsp;"2-digit"。  |
384| second          | string  | 否     | 秒钟的显示格式,取值包括:<br>"numeric",&nbsp;"2-digit"。  |
385| timeZoneName    | string  | 否     | 时区名称的本地化表示,取值包括:<br>"long",&nbsp;"short",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表8](../../internationalization/i18n-time-date.md)。   |
386| dayPeriod       | string  | 否     | 时段的显示格式,取值包括:<br>"long",&nbsp;"short",&nbsp;"narrow",&nbsp;"auto"。<br>不同取值的显示效果请参考[时间日期国际化表10](../../internationalization/i18n-time-date.md)。 |
387| localeMatcher   | string  | 否     | 要使用的区域匹配算法,取值包括:<br>"lookup":精确匹配;<br>"best&nbsp;fit":最佳匹配。 |
388| formatMatcher   | string  | 否     | 要使用的格式匹配算法,取值包括:<br>"basic":精确匹配;<br>"best&nbsp;fit":最佳匹配。 |
389
390## NumberFormat
391
392### constructor<sup>8+</sup>
393
394constructor()
395
396创建数字格式化对象。
397
398**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
399
400**系统能力**:SystemCapability.Global.I18n
401
402**示例:**
403  ```ts
404  // 使用系统当前区域ID创建NumberFormat对象
405  let numfmt = new intl.NumberFormat();
406  ```
407
408
409### constructor
410
411constructor(locale: string | Array&lt;string&gt;, options?: NumberOptions)
412
413创建数字格式化对象。
414
415**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
416
417**系统能力**:SystemCapability.Global.I18n
418
419**参数:**
420
421| 参数名                  | 类型                               | 必填   | 说明                           |
422| -------------------- | -------------------------------- | ---- | ---------------------------- |
423| locale               | string \| Array&lt;string&gt;    | 是    | 表示区域ID的字符串,由语言、脚本、国家或地区组成。 |
424| options              | [NumberOptions](#numberoptions) | 否    | 创建数字格式化对象时可设置的配置项。               |
425
426**示例:**
427  ```ts
428  // 使用en-GB区域ID创建NumberFormat对象,style设置为decimal,notation设置为scientific
429  let numfmt = new intl.NumberFormat("en-GB", {style:'decimal', notation:"scientific"});
430  ```
431
432### format
433
434format(number: number): string
435
436格式化数字字符串。
437
438**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
439
440**系统能力**:SystemCapability.Global.I18n
441
442**参数:**
443
444| 参数名    | 类型     | 必填   | 说明   |
445| ------ | ------ | ---- | ---- |
446| number | number | 是    | 数字对象。 |
447
448**返回值:**
449
450| 类型     | 说明         |
451| ------ | ---------- |
452| string | 格式化后的数字字符串。 |
453
454
455**示例:**
456  ```ts
457  // 使用区域ID列表创建NumberFormat对象,因为en-GB为合法的区域ID,因此使用en-GB创建NumberFormat对象
458  let numfmt : intl.NumberFormat = new intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
459  let formattedNumber : string = numfmt.format(1223); // formattedNumber = 1.223E3
460  let options : intl.NumberOptions = {
461    roundingPriority: "lessPrecision",
462    maximumFractionDigits: 3,
463    maximumSignificantDigits: 3
464  }
465  let numberFmt : intl.NumberFormat = new intl.NumberFormat("en", options);
466  let result : string = numberFmt.format(1.23456); // result = 1.23
467  ```
468
469### formatRange<sup>18+</sup>
470
471formatRange(startRange: number, endRange: number): string
472
473数字范围格式化。
474
475**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。
476
477**系统能力**:SystemCapability.Global.I18n
478
479**参数:**
480
481| 参数名    | 类型     | 必填   | 说明   |
482| ------ | ------ | ---- | ---- |
483| startRange | number | 是    | 开始数字。 |
484| endRange | number | 是    | 结束数字。 |
485
486**返回值:**
487
488| 类型     | 说明         |
489| ------ | ---------- |
490| string | 格式化后的数字范围字符串。 |
491
492
493**示例:**
494  ```ts
495  let numfmt : intl.NumberFormat = new intl.NumberFormat("en-US", {style:'unit', unit:"meter"});
496  let formattedRange : string = numfmt.formatRange(0, 3); // formattedRange: 0–3 m
497  ```
498
499
500### resolvedOptions
501
502resolvedOptions(): NumberOptions
503
504获取创建数字格式化对象时设置的配置项。
505
506**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
507
508**系统能力**:SystemCapability.Global.I18n
509
510**返回值:**
511
512| 类型                               | 说明                          |
513| -------------------------------- | --------------------------- |
514| [NumberOptions](#numberoptions) | 创建数字格式化对象时设置的配置项。 |
515
516
517**示例:**
518  ```ts
519  let numfmt = new intl.NumberFormat(["en-GB", "zh"], {style:'decimal', notation:"scientific"});
520  // 获取NumberFormat对象配置项
521  let options = numfmt.resolvedOptions();
522  let style = options.style; // style = decimal
523  let notation = options.notation; // notation = scientific
524  ```
525
526## NumberOptions
527
528创建数字格式化对象时可设置的配置项。从API9开始,NumberOptions的属性由必填改为可选。
529
530**系统能力**:SystemCapability.Global.I18n
531
532| 名称                       | 类型      | 必填   |  说明                                       |
533| ------------------------ | ------- | ---- |  ---------------------------------------- |
534| locale                   | string  | 否    | 合法的区域ID,&nbsp;如:"zh-Hans-CN"。<br>默认值:系统当前区域ID。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。               |
535| currency                 | string  | 否    | 货币单位,&nbsp;取值符合[ISO-4217标准](https://www.iso.org/iso-4217-currency-codes.html),如:"EUR","CNY","USD"等。<br>从API version 12开始支持三位数字代码,如:"978","156","840"等。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。    |
536| currencySign             | string  | 否    | 货币单位的符号显示,取值包括:&nbsp;"standard","accounting"。<br>默认值为standard。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
537| currencyDisplay          | string  | 否    | 货币的显示方式,取值包括:"symbol",&nbsp;"narrowSymbol",&nbsp;"code",&nbsp;"name"。<br>默认值为symbol。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
538| unit                     | string  | 否    | 单位名称,如:"meter","inch",“hectare”等。<br>从API version 18开始新增支持的组合单位有: "beat-per-minute", "body-weight-per-second", "breath-per-minute", "foot-per-hour", "jump-rope-per-minute", "meter-per-hour", "milliliter-per-minute-per-kilogram", "rotation-per-minute", "step-per-minute", "stroke-per-minute"。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。       |
539| unitDisplay              | string  | 否    | 单位的显示格式,取值包括:"long",&nbsp;"short",&nbsp;"narrow"。<br>默认值为short。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
540| unitUsage<sup>8+</sup>   | string  | 否    | 单位的使用场景,取值包括:"default",&nbsp;"area-land-agricult",&nbsp;"area-land-commercl",&nbsp;"area-land-residntl",&nbsp;"length-person",&nbsp;"length-person-small",&nbsp;"length-rainfall",&nbsp;"length-road",&nbsp;"length-road-small",&nbsp;"length-snowfall",&nbsp;"length-vehicle",&nbsp;"length-visiblty",&nbsp;"length-visiblty-small",&nbsp;"length-person-informal",&nbsp;"length-person-small-informal",&nbsp;"length-road-informal",&nbsp;"speed-road-travel",&nbsp;"speed-wind",&nbsp;"temperature-person",&nbsp;"temperature-weather",&nbsp;"volume-vehicle-fuel",&nbsp;"elapsed-time-second",&nbsp;"size-file-byte",&nbsp;"size-shortfile-byte"。<br>默认值为default。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
541| signDisplay              | string  | 否    | 数字符号的显示格式,取值包括:<br>"auto":自动判断是否显示正负符号;<br>"never":不显示正负号;<br>"always":总是显示正负号;<br>"exceptZero":除了0都显示正负号。<br>默认值为auto。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
542| compactDisplay           | string  | 否    | 紧凑显示格式,取值包括:"long",&nbsp;"short"。<br>默认值为short。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。      |
543| notation                 | string  | 否    | 数字的表示方法,取值包括:"standard",&nbsp;"scientific",&nbsp;"engineering",&nbsp;"compact"。<br>默认值为standard。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
544| localeMatcher            | string  | 否    | 要使用的区域匹配算法,取值包括:"lookup",&nbsp;"best&nbsp;fit"。<br>默认值为best fit。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
545| style                    | string  | 否    | 数字的显示格式,取值包括:"decimal",&nbsp;"currency",&nbsp;"percent",&nbsp;"unit"。<br>默认值为decimal。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
546| numberingSystem          | string  | 否    | 数字系统,取值包括:<br>"adlm",&nbsp;"ahom",&nbsp;"arab",&nbsp;"arabext",&nbsp;"bali",&nbsp;"beng",&nbsp;"bhks",&nbsp;"brah",&nbsp;"cakm",&nbsp;"cham",&nbsp;"deva",&nbsp;"diak",&nbsp;"fullwide",&nbsp;"gong",&nbsp;"gonm",&nbsp;"gujr",&nbsp;"guru",&nbsp;"hanidec",&nbsp;"hmng",&nbsp;"hmnp",&nbsp;"java",&nbsp;"kali",&nbsp;"khmr",&nbsp;"knda",&nbsp;"lana",&nbsp;"lanatham",&nbsp;"laoo",&nbsp;"latn",&nbsp;"lepc",&nbsp;"limb",&nbsp;"mathbold",&nbsp;"mathdbl",&nbsp;"mathmono",&nbsp;"mathsanb",&nbsp;"mathsans",&nbsp;"mlym",&nbsp;"modi",&nbsp;"mong",&nbsp;"mroo",&nbsp;"mtei",&nbsp;"mymr",&nbsp;"mymrshan",&nbsp;"mymrtlng",&nbsp;"newa",&nbsp;"nkoo",&nbsp;"olck",&nbsp;"orya",&nbsp;"osma",&nbsp;"rohg",&nbsp;"saur",&nbsp;"segment",&nbsp;"shrd",&nbsp;"sind",&nbsp;"sinh",&nbsp;"sora",&nbsp;"sund",&nbsp;"takr",&nbsp;"talu",&nbsp;"tamldec",&nbsp;"telu",&nbsp;"thai",&nbsp;"tibt",&nbsp;"tirh",&nbsp;"vaii",&nbsp;"wara",&nbsp;"wcho"。numberingSystem属性默认值为区域的默认数字系统。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
547| useGrouping              | boolean | 否    | true表示分组显示,false表示不分组显示。useGrouping属性默认值为true。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                                  |
548| minimumIntegerDigits     | number  | 否    | 表示要使用的最小整数位数,取值范围:1~21。<br>minimumIntegerDigits属性默认值为1。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                  |
549| minimumFractionDigits    | number  | 否    | 表示要使用的最小分数位数,取值范围:0~20。<br>minimumFractionDigits属性默认值为0。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                  |
550| maximumFractionDigits    | number  | 否    | 表示要使用的最大分数位数,取值范围:1~21。<br>maximumFractionDigits属性默认值为3。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                  |
551| minimumSignificantDigits | number  | 否    | 表示要使用的最小有效位数,取值范围:1~21。<br>minimumSignificantDigits属性默认值为1。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                  |
552| maximumSignificantDigits | number  | 否    | 表示要使用的最大有效位数,取值范围:1~21。<br>maximumSignificantDigits属性默认值为21。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                  |
553| roundingPriority<sup>18+</sup>   | string  | 否    | 最大分数位数和最大有效位数同时设置时的舍入优先级,取值包括:"auto","morePrecision"&nbsp;取最大分数位数,"lessPrecision"&nbsp;取最大有效位数。<br>roundingPriority默认值为auto。<br>**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。                  |
554| roundingIncrement<sup>18+</sup>  | number  | 否    | 表示舍入增量,取值范围:1,2,5,10,20,25,50,100,200,250,500,1000,2000,2500,5000。<br>roundingIncrement属性默认值为1。<br>**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。                  |
555| roundingMode<sup>18+</sup>       | string  | 否    | 表示舍入模式,取值包括:<br>"ceil":向上取整;<br>"floor":向下取整;<br>"expand":远离零取整;<br>"trunc":向零取整;<br>"halfCeil":半向上取整,大于等于增量的一半时向上取整,小于增量的一半时向下取整;<br>"halfFloor":半向下取整,大于增量的一半时向上取整,小于等于增量的一半时向下取整;<br>"halfExpand":半远离零取整,大于等于增量的一半时远离零取整,小于增量的一半时向零取整;<br>"halfTrunc":半向零取整,大于增量的一半时远离零取整,小于等于增量的一半时向零取整;<br>"halfEven":半向偶数取整,大于半增量的一半时 远离零取整,小于增量的一半时向零取整,等于增量的一半时向最近的偶数位舍入。<br>roundingMode属性默认值为halfExpand。<br>**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 |
556
557>  **说明:**
558>
559>  - 各属性不同取值代表的含义或呈现效果,请参考[数字与度量衡国际化](../../internationalization/i18n-numbers-weights-measures.md)。
560
561## Collator<sup>8+</sup>
562
563### constructor<sup>8+</sup>
564
565constructor()
566
567创建排序对象。
568
569**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
570
571**系统能力**:SystemCapability.Global.I18n
572
573**示例:**
574  ```ts
575  // 使用系统区域创建Collator对象
576  let collator = new intl.Collator();
577  ```
578
579
580### constructor<sup>8+</sup>
581
582constructor(locale: string | Array&lt;string&gt;, options?: CollatorOptions)
583
584创建排序对象。
585
586**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
587
588**系统能力**:SystemCapability.Global.I18n
589
590**参数:**
591
592| 参数名                  | 类型                                   | 必填   | 说明                           |
593| -------------------- | ------------------------------------ | ---- | ---------------------------- |
594| locale               | string \| Array&lt;string&gt;        | 是    | 表示区域ID的字符串,由语言、脚本、国家或地区组成。  |
595| options              | [CollatorOptions](#collatoroptions8) | 否    | 创建排序对象时可设置的配置项。       |
596
597**示例:**
598  ```ts
599  // 使用zh-CN区域ID创建Collator对象,localeMatcher设置为lookup,usage设置为sort
600  let collator = new intl.Collator("zh-CN", {localeMatcher: "lookup", usage: "sort"});
601  ```
602
603
604### compare<sup>8+</sup>
605
606compare(first: string, second: string): number
607
608依据配置项设置的排序规则,比较两个字符串。
609
610**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
611
612**系统能力**:SystemCapability.Global.I18n
613
614**参数:**
615
616| 参数名    | 类型     | 必填   | 说明           |
617| ------ | ------ | ---- | ------------ |
618| first  | string | 是    | 进行比较第一个字符串。  |
619| second | string | 是    | 进行比较的第二个字符串。 |
620
621**返回值:**
622
623| 类型     | 说明                                       |
624| ------ | ---------------------------------------- |
625| number | 比较结果。<br>- number为负数时,表示first排序在second之前;<br>- number为0时,表示first与second排序相同;<br>- number为正数,表示first排序在second之后。 |
626
627**示例:**
628  ```ts
629  // 使用en-GB区域ID创建Collator对象
630  let collator = new intl.Collator("en-GB");
631  // 比较first和second的先后顺序
632  let compareResult = collator.compare("first", "second"); // compareResult = -1
633  ```
634
635
636### resolvedOptions<sup>8+</sup>
637
638resolvedOptions(): CollatorOptions
639
640获取创建排序对象时设置的配置项。
641
642**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
643
644**系统能力**:SystemCapability.Global.I18n
645
646**返回值:**
647
648| 类型                                   | 说明                |
649| ------------------------------------ | ----------------- |
650| [CollatorOptions](#collatoroptions8) | 返回排序对象的属性。 |
651
652**示例:**
653  ```ts
654  let collator = new intl.Collator("zh-Hans", { usage: 'sort', ignorePunctuation: true });
655  // 获取Collator对象的配置项
656  let options = collator.resolvedOptions();
657  let usage = options.usage; // usage = "sort"
658  let ignorePunctuation = options.ignorePunctuation; // ignorePunctuation = true
659  ```
660
661
662## CollatorOptions<sup>8+</sup>
663
664创建排序对象时可设置的配置项。
665
666从API9中,CollatorOptions中的属性改为可选。
667
668**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
669
670**系统能力**:SystemCapability.Global.I18n
671
672| 名称                | 类型      | 必填   | 说明                                       |
673| ----------------- | ------- | ---- | ---------------------------------------- |
674| localeMatcher     | string  | 否    | locale匹配算法,取值范围:"best&nbsp;fit",&nbsp;"lookup"。<br>默认值为best fit。 |
675| usage             | string  | 否    | 比较的用途,取值范围:"sort",&nbsp;"search"。<br>默认值为sort。        |
676| sensitivity       | string  | 否    | 表示字符串中的哪些差异会导致非零结果值,取值范围:"base",&nbsp;"accent",&nbsp;"case",&nbsp;"letiant"。<br>默认值为variant。 |
677| ignorePunctuation | boolean | 否    | true表示忽略标点符号,false表示考虑标点符号。<br>默认值为false。        |
678| collation         | string  | 否    | 排序规则,<br>取值范围:"big5han",&nbsp;"compat",&nbsp;"dict",&nbsp;"direct",&nbsp;"ducet",&nbsp;"eor",&nbsp;"gb2312",&nbsp;"phonebk",&nbsp;"phonetic",&nbsp;"pinyin",&nbsp;"reformed",&nbsp;"searchjl",&nbsp;"stroke",&nbsp;"trad",&nbsp;"unihan",&nbsp;"zhuyin"。<br>默认值为default。 |
679| numeric           | boolean | 否    | true表示使用数字排序,false表示不使用数字排序。<br>默认值为false。          |
680| caseFirst         | string  | 否    | 表示大写、小写的排序顺序,取值范围:"upper",&nbsp;"lower",&nbsp;"false"。<br>默认值为false。 |
681
682
683>  **说明:**
684>
685>  - CollatorOptions中属性的不同取值代表的含义请参考[本地习惯排序](../../internationalization/i18n-sorting-local.md)。
686
687
688## PluralRules<sup>8+</sup>
689
690### constructor<sup>8+</sup>
691
692constructor()
693
694创建单复数对象来计算数字的单复数类别。
695
696**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
697
698**系统能力**:SystemCapability.Global.I18n
699
700**示例:**
701  ```ts
702  // 使用系统区域创建PluralRules对象
703  let pluralRules = new intl.PluralRules();
704  ```
705
706
707### constructor<sup>8+</sup>
708
709constructor(locale: string | Array&lt;string&gt;, options?: PluralRulesOptions)
710
711创建单复数对象来计算数字的单复数类别。
712
713**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
714
715**系统能力**:SystemCapability.Global.I18n
716
717**参数:**
718
719| 参数名                  | 类型                                       | 必填   | 说明                           |
720| -------------------- | ---------------------------------------- | ---- | ---------------------------- |
721| locale               | string \| Array&lt;string&gt;            | 是    | 表示区域ID的字符串,由语言、脚本、国家或地区组成。 |
722| options              | [PluralRulesOptions](#pluralrulesoptions8) | 否    | 创建单复数对象时设置的配置项。       |
723
724**示例:**
725  ```ts
726  // 使用zh-CN区域ID创建PluralRules对象,localeMatcher设置为lookup,type设置为cardinal
727  let pluralRules= new intl.PluralRules("zh-CN", {"localeMatcher": "lookup", "type": "cardinal"});
728  ```
729
730### select<sup>8+</sup>
731
732select(n: number): string
733
734返回一个字符串表示该数字的单复数类别。
735
736**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
737
738**系统能力**:SystemCapability.Global.I18n
739
740**参数:**
741
742| 参数名  | 类型     | 必填   | 说明           |
743| ---- | ------ | ---- | ------------ |
744| n    | number | 是    | 待获取单复数类别的数字。 |
745
746**返回值:**
747
748| 类型     | 说明                                       |
749| ------ | ---------------------------------------- |
750| string | 单复数类别,取值包括:"zero","one","two",&nbsp;"few",&nbsp;"many",&nbsp;"others"。 <br>不同取值代表的含义请参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。|
751
752**示例:**
753  ```ts
754  // 使用zh-Hans区域ID创建PluralRules对象
755  let zhPluralRules = new intl.PluralRules("zh-Hans");
756  // 计算zh-Hans区域中数字1对应的单复数类别
757  let plural = zhPluralRules.select(1); // plural = other
758
759  // 使用en-US区域ID创建PluralRules对象
760  let enPluralRules = new intl.PluralRules("en-US");
761  // 计算en-US区域中数字1对应的单复数类别
762  plural = enPluralRules.select(1); // plural = one
763  ```
764
765
766## PluralRulesOptions<sup>8+</sup>
767
768创建单复数对象时可设置的配置项。从API9开始,PluralRulesOptions的属性由必填改为可选。
769
770**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
771
772**系统能力**:SystemCapability.Global.I18n
773
774| 名称                       | 类型     | 可读   | 可写   | 说明                                       |
775| ------------------------ | ------ | ---- | ---- | ---------------------------------------- |
776| localeMatcher            | string | 是    | 是    | locale匹配算法,取值包括:"best&nbsp;fit",&nbsp;"lookup"。<br>默认值为best fit。 |
777| type                     | string | 是    | 是    | 排序的类型,取值包括:"cardinal",&nbsp;"ordinal",<br>默认值为cardinal。<br>- cardinal:基数词,ordinal:序数词。  |
778| minimumIntegerDigits     | number | 是    | 是    | 表示要使用的最小整数位数,取值范围:1~21。<br>默认值为1。                  |
779| minimumFractionDigits    | number | 是    | 是    | 表示要使用的最小分数位数,取值范围:0~20。<br>默认值为0。                  |
780| maximumFractionDigits    | number | 是    | 是    | 表示要使用的最大分数位数,取值范围:1~21。<br>默认值为3。                  |
781| minimumSignificantDigits | number | 是    | 是    | 表示要使用的最小有效位数,取值范围:1~21。<br>默认值为1。                  |
782| maximumSignificantDigits | number | 是    | 是    | 表示要使用的最大有效位数,取值范围:1~21。<br>默认值为21。                |
783
784
785## RelativeTimeFormat<sup>8+</sup>
786
787### constructor<sup>8+</sup>
788
789constructor()
790
791创建相对时间格式化对象。
792
793**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
794
795**系统能力**:SystemCapability.Global.I18n
796
797**示例:**
798  ```ts
799  // 使用系统区域创建RelativeTimeFormat对象
800  let relativetimefmt = new intl.RelativeTimeFormat();
801  ```
802
803
804### constructor<sup>8+</sup>
805
806constructor(locale: string | Array&lt;string&gt;, options?: RelativeTimeFormatInputOptions)
807
808创建相对时间格式化对象。
809
810**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
811
812**系统能力**:SystemCapability.Global.I18n
813
814**参数:**
815
816| 参数名                  | 类型                                       | 必填   | 说明                           |
817| -------------------- | ---------------------------------------- | ---- | ---------------------------- |
818| locale               | string \| Array&lt;string&gt;            | 是    | 表示区域ID的字符串,由语言、脚本、国家或地区组成。 |
819| options              | [RelativeTimeFormatInputOptions](#relativetimeformatinputoptions8) | 否    | 创建相对时间格式化对象时可配置的选项。     |
820
821**示例:**
822  ```ts
823  // 使用zh-CN区域ID创建RelativeTimeFormat对象,localeMatcher设置为lookup,numeric设置为always,style设置为long
824  let relativeTimeFormat = new intl.RelativeTimeFormat("zh-CN", {"localeMatcher": "lookup", "numeric": "always", "style": "long"});
825  ```
826
827
828### format<sup>8+</sup>
829
830format(value: number, unit: string): string
831
832依据区域ID和格式化选项,对value和unit进行格式化。
833
834**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
835
836**系统能力**:SystemCapability.Global.I18n
837
838**参数:**
839
840| 参数名   | 类型     | 必填   | 说明                                       |
841| ----- | ------ | ---- | ---------------------------------------- |
842| value | number | 是    | 相对时间格式化的数值。                              |
843| unit  | string | 是    | 相对时间格式化的单位,<br>取值包括:"year",&nbsp;"quarter",&nbsp;"month",&nbsp;"week",&nbsp;"day",&nbsp;"hour",&nbsp;"minute",&nbsp;"second"。 |
844
845**返回值:**
846
847| 类型     | 说明         |
848| ------ | ---------- |
849| string | 格式化后的相对时间。 |
850
851**示例:**
852  ```ts
853  // 使用zh-CN区域ID创建RelativeTimeFormat对象
854  let relativetimefmt = new intl.RelativeTimeFormat("zh-CN");
855  // 计算zh-CN区域中数字3,单位quarter的本地化表示
856  let formatResult = relativetimefmt.format(3, "quarter"); // formatResult = "3个季度后"
857  ```
858
859
860### formatToParts<sup>8+</sup>
861
862formatToParts(value: number, unit: string): Array&lt;object&gt;
863
864自定义区域的相对时间格式。
865
866**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
867
868**系统能力**:SystemCapability.Global.I18n
869
870**参数:**
871
872| 参数名   | 类型     | 必填   | 说明                                       |
873| ----- | ------ | ---- | ---------------------------------------- |
874| value | number | 是    | 相对时间格式化的数值。                              |
875| unit  | string | 是    | 相对时间格式化的单位,<br>取值包括:"year",&nbsp;"quarter",&nbsp;"month",&nbsp;"week",&nbsp;"day",&nbsp;"hour",&nbsp;"minute",&nbsp;"second"。 |
876
877**返回值:**
878
879| 类型                  | 说明                          |
880| ------------------- | --------------------------- |
881| Array&lt;object&gt; | 相对时间格式的对象数组。 |
882
883**示例:**
884  ```ts
885  // 使用en区域ID创建RelativeTimeFormat对象,numeric设置为auto
886  let relativetimefmt = new intl.RelativeTimeFormat("en", {"numeric": "auto"});
887  let parts = relativetimefmt.formatToParts(10, "seconds"); // parts = [ {type: "literal", value: "in"}, {type: "integer", value: 10, unit: "second"}, {type: "literal", value: "seconds"} ]
888  ```
889
890
891### resolvedOptions<sup>8+</sup>
892
893resolvedOptions(): RelativeTimeFormatResolvedOptions
894
895获取RelativeTimeFormat对象的格式化选项。
896
897**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
898
899**系统能力**:SystemCapability.Global.I18n
900
901**返回值:**
902
903| 类型                                       | 说明                                |
904| ---------------------------------------- | --------------------------------- |
905| [RelativeTimeFormatResolvedOptions](#relativetimeformatresolvedoptions8) | RelativeTimeFormat&nbsp;对象的格式化选项。 |
906
907**示例:**
908  ```ts
909  // 使用en-GB区域ID创建RelativeTimeFormat对象
910  let relativetimefmt= new intl.RelativeTimeFormat("en-GB", { style: "short" });
911  // 获取RelativeTimeFormat对象配置项
912  let options = relativetimefmt.resolvedOptions();
913  let style = options.style; // style = "short"
914  ```
915
916
917## RelativeTimeFormatInputOptions<sup>8+</sup>
918
919创建相对时间格式化对象时可设置的属性选项。
920
921从API9开始,RelativeTimeFormatInputOptions中的属性改为可选。
922
923**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
924
925**系统能力**:SystemCapability.Global.I18n
926
927| 名称            | 类型     | 必填   |说明                                       |
928| ------------- | ------ | ---- | ---------------------------------------- |
929| localeMatcher | string | 否    | locale匹配算法,取值包括:"best&nbsp;fit",&nbsp;"lookup"。<br>默认值为best fit。 |
930| numeric       | string | 否    | 输出消息的格式,取值包括:"always",&nbsp;"auto"。<br>默认值为always。      |
931| style         | string | 否    | 国际化消息的长度,取值包括:"long",&nbsp;"short",&nbsp;"narrow"。<br>默认值为long。 |
932
933> **说明**
934>
935> numeric、style不同参数取值显示的效果,请参考[相对时间格式化选项](../../internationalization/i18n-time-date.md#相对时间格式化)。
936
937## RelativeTimeFormatResolvedOptions<sup>8+</sup>
938
939表示RelativeTimeFormat对象可设置的属性。
940
941**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
942
943**系统能力**:SystemCapability.Global.I18n
944
945| 名称              | 类型     | 必填   |说明                                       |
946| --------------- | ------ | ---- | ---------------------------------------- |
947| locale          | string | 是    | 表示区域ID的字符串,包括语言以及可选的脚本和区域。             |
948| numeric         | string | 是    | 输出消息的格式,取值包括:"always",&nbsp;"auto"。      |
949| style           | string | 是    | 国际化消息的长度,取值包括:"long",&nbsp;"short",&nbsp;"narrow"。 |
950| numberingSystem | string | 是    | 使用的数字系统,取值包括:<br>"adlm",&nbsp;"ahom",&nbsp;"arab",&nbsp;"arabext",&nbsp;"bali",&nbsp;"beng",&nbsp;"bhks",&nbsp;"brah",&nbsp;"cakm",&nbsp;"cham",&nbsp;"deva",&nbsp;"diak",&nbsp;"fullwide",&nbsp;"gong",&nbsp;"gonm",&nbsp;"gujr",&nbsp;"guru",&nbsp;"hanidec",&nbsp;"hmng",&nbsp;"hmnp",&nbsp;"java",&nbsp;"kali",&nbsp;"khmr",&nbsp;"knda",&nbsp;"lana",&nbsp;"lanatham",&nbsp;"laoo",&nbsp;"latn",&nbsp;"lepc",&nbsp;"limb",&nbsp;"mathbold",&nbsp;"mathdbl",&nbsp;"mathmono",&nbsp;"mathsanb",&nbsp;"mathsans",&nbsp;"mlym",&nbsp;"modi",&nbsp;"mong",&nbsp;"mroo",&nbsp;"mtei",&nbsp;"mymr",&nbsp;"mymrshan",&nbsp;"mymrtlng",&nbsp;"newa",&nbsp;"nkoo",&nbsp;"olck",&nbsp;"orya",&nbsp;"osma",&nbsp;"rohg",&nbsp;"saur",&nbsp;"segment",&nbsp;"shrd",&nbsp;"sind",&nbsp;"sinh",&nbsp;"sora",&nbsp;"sund",&nbsp;"takr",&nbsp;"talu",&nbsp;"tamldec",&nbsp;"telu",&nbsp;"thai",&nbsp;"tibt",&nbsp;"tirh",&nbsp;"vaii",&nbsp;"wara",&nbsp;"wcho"。 |
951
952> **说明**
953>
954> numeric、style不同参数取值显示的效果,请参考[相对时间格式化选项](../../internationalization/i18n-time-date.md#相对时间格式化)。