1# @arkts.math.Decimal (高精度数学库Decimal)
2
3Decimal用于提供高精度数学库,主要用于提供高精度浮点运算能力。
4
5> **说明:**
6>
7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10## 导入模块
11
12```ts
13import { Decimal } from '@kit.ArkTS';
14```
15
16## Value
17
18type Value = string | number | Decimal
19
20表示用于构建Decimal的参数类型。
21
22取值类型为下列类型中的并集。
23
24**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
25
26**系统能力**:SystemCapability.Utils.Lang
27
28| 类型                | 说明                           |
29| ------------------- | ------------------------------ |
30| string              | 表示值类型为字符,可取任意值。 |
31| number              | 表示值类型为数字,可取任意值。 |
32| [Decimal](#decimal) | 表示值类型为Decimal类型。      |
33
34## Rounding
35
36type Rounding = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
37
38表示可设置的舍入类型。
39
40取值类型为下列类型中的并集。
41
42**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
43
44**系统能力**:SystemCapability.Utils.Lang
45
46| 类型 | 说明                                                         |
47| ---- | ------------------------------------------------------------ |
48| 0    | 向远离零的方向舍入。与[Decimal.ROUND_UP](#常量)一致。        |
49| 1    | 向靠近零的方向舍入。与[Decimal.ROUND_DOWN](#常量)一致。      |
50| 2    | 向正无穷方向舍入。与[Decimal.ROUND_CEILING](#常量)一致。     |
51| 3    | 向负无穷方向舍入。与[Decimal.ROUND_FLOOR](#常量)一致。       |
52| 4    | 向最近的邻值舍入。如果距离相等,则远离零方向舍入。与[Decimal.ROUND_HALF_UP](#常量)一致。 |
53| 5    | 向最近的邻值舍入。如果距离相等,则靠近零方向舍入。与[Decimal.ROUND_HALF_DOWN](#常量)一致。 |
54| 6    | 向最近的邻值舍入。如果距离相等,则向偶数邻值舍入。与[Decimal.ROUND_HALF_EVEN](#常量)一致。 |
55| 7    | 向最近的邻值舍入。如果距离相等,则向正无穷方向舍入。与[Decimal.ROUND_HALF_CEILING](#常量)一致。 |
56| 8    | 向最近的邻值舍入。如果距离相等,则向负无穷方向舍入。与[Decimal.ROUND_HALF_FLOOR](#常量)一致。 |
57
58## Modulo
59
60type Modulo = Rounding | 9
61
62表示可设置的取模方法舍入类型。
63
64取值类型为下列类型中的并集。
65
66**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
67
68**系统能力**:SystemCapability.Utils.Lang
69
70| 类型                   | 说明                                                         |
71| ---------------------- | ------------------------------------------------------------ |
72| [Rounding](#rounding) | 模运算下的舍入类型。与[Rounding](#常量)表示的舍入模式相同。  |
73| 9                      | 余模运算下,余数始终为正。欧几里得除法。与[Decimal.EUCLID](#常量)一致。 |
74
75## DecimalConfig
76
77用于设置Decimal的配置属性,可使用[Decimal.set](#set)方法进行配置。
78
79### 属性
80
81**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
82
83**系统能力**:SystemCapability.Utils.Lang
84
85| 名称      | 类型                   | 只读 | 必填 | 说明                                                         |
86| --------- | ---------------------- | ---- | ---- | ------------------------------------------------------------ |
87| precision | number                 | 否   | 否   | 运算结果的最大有效位数,取值范围为[1, 1e9],默认值为20。     |
88| rounding  | [Rounding](#rounding) | 否   | 否   | 舍入模式,取值范围为0到8的整数,默认值为4。                  |
89| toExpNeg  | number                 | 否   | 否   | 指数表示法的负指数值的极限值,若Decimal的负指数小于等于该值时,使用科学计数法表示,[toString](#tostring)方法中使用,取值范围为[-9e15, 0],默认值为-7。 |
90| toExpPos  | number                 | 否   | 否   | 指数表示法的正指数值的极限值,若Decimal的正指数大于等于该值时,使用科学计数法表示,[toString](#tostring)方法中使用,取值范围为[0, 9e15],默认值为20。 |
91| minE      | number                 | 否   | 否   | 负指数极限,若Decimal的指数值小于该值,会下溢到零,取值范围为[-9e15, 0],默认值为-9e15。 |
92| maxE      | number                 | 否   | 否   | 正指数极限,若Decimal的指数值大于该值,会溢出至无穷大,取值范围为[0, 9e15],默认值为9e15。 |
93| crypto    | boolean                | 否   | 否   | 确定是否使用加密安全伪随机数生成的值,用默认值为false。      |
94| modulo    | [Modulo](#modulo)      | 否   | 否   | 模计算时使用的舍入模式,取值范围为0到9的整数,默认值为1。    |
95| defaults  | boolean                | 否   | 否   | 表示未指定的属性是否被设置为默认值,true表示使用默认值,false表示不使用默认值,默认值为false。 |
96
97## Decimal
98
99任意精度的Decimal类型。
100
101### 属性
102
103**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
104
105**系统能力**:SystemCapability.Utils.Lang
106
107| 名称 | 类型     | 只读 | 必填 | 说明                                      |
108| ---- | -------- | ---- | ---- | ----------------------------------------- |
109| d    | number[] | 是   | 是   | digits:表示Decimal数整数部分和小数部分。 |
110| e    | number   | 是   | 是   | exponent:表示Decimal数十进制指数的数目。 |
111| s    | number   | 是   | 是   | sign:表示Decimal数的符号位。             |
112
113### 常量
114
115**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
116
117**系统能力**:SystemCapability.Utils.Lang
118
119| 名称               | 类型   | 值   | 说明                                                         |
120| ------------------ | ------ | ---- | ------------------------------------------------------------ |
121| ROUND_UP           | number | 0    | 向远离零的方向舍入。模运算下,如果被除数为负,则余数为正,否则为负。 |
122| ROUND_DOWN         | number | 1    | 向靠近零的方向舍入。模运算下,余数与被除数的符号相同,使用截断除法。 |
123| ROUND_CEILING      | number | 2    | 向正无穷方向舍入。                                           |
124| ROUND_FLOOR        | number | 3    | 向负无穷方向舍入。模运算下,余数与除数的符号相同。           |
125| ROUND_HALF_UP      | number | 4    | 向最近的邻值舍入。如果距离相等,则远离零方向舍入。           |
126| ROUND_HALF_DOWN    | number | 5    | 向最近的邻值舍入。如果距离相等,则靠近零方向舍入。           |
127| ROUND_HALF_EVEN    | number | 6    | 向最近的邻值舍入。如果距离相等,则向偶数邻值舍入。模运算下,IEEE 754 求余函数。 |
128| ROUND_HALF_CEILING | number | 7    | 向最近的邻值舍入。如果距离相等,则向正无穷方向舍入。         |
129| ROUND_HALF_FLOOR   | number | 8    | 向最近的邻值舍入。如果距离相等,则向负无穷方向舍入。         |
130| EUCLID             | number | 9    | 模运算下,余数始终为正。使用欧几里得除法:q = sign(x) * floor(a / abs(x))。 |
131
132### constructor
133
134constructor(n: Value)
135
136Decimal的构造函数。
137
138**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
139
140**系统能力**:SystemCapability.Utils.Lang
141
142**参数:**
143
144| 参数名 | 类型            | 必填 | 说明                    |
145| ------ | --------------- | ---- | ----------------------- |
146| n      | [Value](#value) | 是   | 构造Decimal时的初始值。 |
147
148**错误码:**
149
150以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
151
152| 错误码ID | 错误信息                                                     |
153| -------- | ------------------------------------------------------------ |
154| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
155
156**示例:**
157
158```ts
159let a: Decimal = new Decimal(5);
160console.info("test Decimal constructor:" + a.toString()); // '5'
161```
162
163### abs
164
165abs(): Decimal
166
167返回一个新的Decimal对象,其值为该Decimal的绝对值。
168
169**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
170
171**系统能力**:SystemCapability.Utils.Lang
172
173**返回值:**
174
175| 类型                | 说明                                |
176| ------------------- | ----------------------------------- |
177| [Decimal](#decimal) | 返回绝对值运算后的Decimal对象实例。 |
178
179**示例:**
180
181```ts
182let a: Decimal = new Decimal(-0.5).abs();
183console.info("test Decimal abs:" + a.toString()); // '0.5'
184```
185
186### floor
187
188floor(): Decimal
189
190返回一个新的Decimal对象,其值为该Decimal向负无穷方向舍入得到的结果。
191
192**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
193
194**系统能力**:SystemCapability.Utils.Lang
195
196**返回值:**
197
198| 类型                | 说明                            |
199| ------------------- | ------------------------------- |
200| [Decimal](#decimal) | 返回舍入之后的Decimal对象实例。 |
201
202**示例:**
203
204```ts
205let a: Decimal = new Decimal(1.8).floor();
206console.info("test Decimal floor:" + a.toString()); // '1'
207```
208
209### ceil
210
211ceil(): Decimal
212
213返回一个新的Decimal对象,其值为该Decimal向正无穷方向舍入得到的结果。
214
215**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
216
217**系统能力**:SystemCapability.Utils.Lang
218
219**返回值:**
220
221| 类型                | 说明                            |
222| ------------------- | ------------------------------- |
223| [Decimal](#decimal) | 返回舍入之后的Decimal对象实例。 |
224
225**示例:**
226
227```ts
228let a: Decimal = new Decimal(1.8).ceil();
229console.info("test Decimal ceil:" + a.toString()); // '2'
230```
231
232### trunc
233
234trunc(): Decimal
235
236返回一个新的Decimal,其值是将此Decimal截断为整数部分。
237
238**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
239
240**系统能力**:SystemCapability.Utils.Lang
241
242**返回值:**
243
244| 类型                | 说明                            |
245| ------------------- | ------------------------------- |
246| [Decimal](#decimal) | 返回截断之后的Decimal对象实例。 |
247
248**示例:**
249
250```ts
251let a: Decimal = new Decimal(2.5).trunc();
252console.info("test Decimal trunc:" + a.toString()); // '2'
253```
254
255### clamp
256
257clamp(min: Value, max: Value): Decimal
258
259返回一个值为将该Decimal的值限制在min到max范围内的Decimal对象。
260
261**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
262
263**系统能力**:SystemCapability.Utils.Lang
264
265**参数:**
266
267| 参数名 | 类型            | 必填 | 说明                     |
268| ------ | --------------- | ---- | ------------------------ |
269| min    | [Value](#value) | 是   | 限制的最小值。包含该值。 |
270| max    | [Value](#value) | 是   | 限制的最大值。包含该值。 |
271
272**返回值:**
273
274| 类型                | 说明                              |
275| ------------------- | --------------------------------- |
276| [Decimal](#decimal) | 返回符合范围内的Decimal对象实例。 |
277
278**错误码:**
279
280以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
281
282| 错误码ID | 错误信息                                                     |
283| -------- | ------------------------------------------------------------ |
284| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
285| 10200001 | The value of 'min' is out of range.                          |
286
287**示例:**
288
289```ts
290let a: Decimal = new Decimal(10.1).clamp(0, 10);
291console.info("test Decimal clamp:" + a.toString()); // '10'
292```
293
294
295
296### add
297
298add(n: Value): Decimal;
299
300返回一个新的Decimal,其值为该Decimal的值加上n。
301
302使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
303
304**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
305
306**系统能力**:SystemCapability.Utils.Lang
307
308**参数:**
309
310| 参数名 | 类型            | 必填 | 说明             |
311| ------ | --------------- | ---- | ---------------- |
312| n      | [Value](#value) | 是   | 加法运算的加数。 |
313
314**返回值**:
315
316| 类型    | 说明                            |
317| ------- | ------------------------------- |
318| [Decimal](#decimal) | 返回加法运算后的Decimal对象实例。 |
319
320**错误码:**
321
322以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
323
324| 错误码ID | 错误信息                                                     |
325| -------- | ------------------------------------------------------------ |
326| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
327
328**示例:**
329
330```ts
331let a: Decimal = new Decimal(0.5).add(0.5);
332console.info("test Decimal add:" + a.toString()); // '1'
333```
334
335### sub
336
337sub(n: Value): Decimal
338
339返回一个新的Decimal,其值为此Decimal的值减去n。
340
341使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
342
343**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
344
345**系统能力**:SystemCapability.Utils.Lang
346
347**参数:**
348
349| 参数名 | 类型            | 必填 | 说明             |
350| ------ | --------------- | ---- | ---------------- |
351| n      | [Value](#value) | 是   | 减法运算的减数。 |
352
353**返回值:**
354
355| 类型    | 说明                            |
356| ------- | ------------------------------- |
357| [Decimal](#decimal) | 返回减法运算后的Decimal对象实例。 |
358
359**错误码:**
360
361以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
362
363| 错误码ID | 错误信息                                                     |
364| -------- | ------------------------------------------------------------ |
365| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
366
367**示例:**
368
369```ts
370let a: Decimal = new Decimal(1).sub(0.5);
371console.info("test Decimal sub:" + a.toString()); // '0.5'
372```
373
374### mul
375
376mul(n: Value): Decimal
377
378返回一个新的Decimal,其值为Decimal乘以n。
379
380使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
381
382**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
383
384**系统能力**:SystemCapability.Utils.Lang
385
386**参数:**
387
388| 参数名 | 类型            | 必填 | 说明             |
389| ------ | --------------- | ---- | ---------------- |
390| n      | [Value](#value) | 是   | 乘法运算的乘数。 |
391
392**返回值:**
393
394| 类型    | 说明                            |
395| ------- | ------------------------------- |
396| [Decimal](#decimal) | 返回乘法运算后的Decimal对象实例。 |
397
398**错误码:**
399
400以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
401
402| 错误码ID | 错误信息                                                     |
403| -------- | ------------------------------------------------------------ |
404| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
405
406**示例:**
407
408```ts
409let a: Decimal = new Decimal(1).mul(0.5);
410console.info("test Decimal mul:" + a.toString()); // '0.5'
411```
412
413### div
414
415div(n: Value): Decimal
416
417返回一个新的Decimal,其值是此Decimal的值除以n。
418
419使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
420
421**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
422
423**系统能力**:SystemCapability.Utils.Lang
424
425**参数:**
426
427| 参数名 | 类型            | 必填 | 说明             |
428| ------ | --------------- | ---- | ---------------- |
429| n      | [Value](#value) | 是   | 除法运算的除数。 |
430
431**返回值:**
432
433| 类型    | 说明                            |
434| ------- | ------------------------------- |
435| [Decimal](#decimal) | 返回除法运算后的Decimal对象实例。 |
436
437**错误码:**
438
439以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
440
441| 错误码ID | 错误信息                                                     |
442| -------- | ------------------------------------------------------------ |
443| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
444
445**示例:**
446
447```ts
448let a: Decimal = new Decimal(1).div(0.5);
449console.info("test Decimal div:" + a.toString()); // '2'
450```
451
452### mod
453
454mod(n: Value): Decimal
455
456返回一个新的Decimal,其值是该Decimal除以n的模。
457
458使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
459
460**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
461
462**系统能力**:SystemCapability.Utils.Lang
463
464**参数:**
465
466| 参数名 | 类型            | 必填 | 说明             |
467| ------ | --------------- | ---- | ---------------- |
468| n      | [Value](#value) | 是   | 取模运算的除数。 |
469
470**返回值:**
471
472| 类型    | 说明                            |
473| ------- | ------------------------------- |
474| [Decimal](#decimal) | 返回取模运算后的Decimal对象实例。 |
475
476**错误码:**
477
478以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
479
480| 错误码ID | 错误信息                                                     |
481| -------- | ------------------------------------------------------------ |
482| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
483
484**示例:**
485
486```ts
487let a: Decimal = new Decimal(2).mod(1);
488console.info("test Decimal mod:" + a.toString()); // '0'
489```
490
491### sqrt
492
493sqrt(): Decimal
494
495返回一个新的Decimal,其值为该Decimal的平方根。
496
497使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
498
499**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
500
501**系统能力**:SystemCapability.Utils.Lang
502
503**返回值:**
504
505| 类型    | 说明                              |
506| ------- | --------------------------------- |
507| [Decimal](#decimal) | 返回平方根运算后的Decimal对象实例。 |
508
509**示例:**
510
511```ts
512let a: Decimal = new Decimal(3).sqrt();
513console.info("test Decimal sqrt:" + a.toString()); // '1.7320508075688772935'
514```
515
516### cbrt
517
518cbrt(): Decimal
519
520返回一个新的Decimal,其值是此Decimal值的立方根。
521
522使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
523
524**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
525
526**系统能力**:SystemCapability.Utils.Lang
527
528**返回值:**
529
530| 类型    | 说明                              |
531| ------- | --------------------------------- |
532| [Decimal](#decimal) | 返回立方根运算后的Decimal对象实例。 |
533
534**示例:**
535
536```ts
537let a: Decimal = new Decimal(3).cbrt();
538console.info("test Decimal cbrt:" + a.toString()); // '1.4422495703074083823'
539```
540
541### pow
542
543pow(n: Value): Decimal
544
545返回一个新的Decimal,它的值是这个Decimal的值的n次幂。
546
547使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
548
549**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
550
551**系统能力**:SystemCapability.Utils.Lang
552
553**参数:**
554
555| 参数名 | 类型            | 必填 | 说明             |
556| ------ | --------------- | ---- | ---------------- |
557| n      | [Value](#value) | 是   | 幂运算的幂的值。 |
558
559**返回值:**
560
561| 类型    | 说明                          |
562| ------- | ----------------------------- |
563| [Decimal](#decimal) | 返回幂运算后的Decimal对象实例。 |
564
565**错误码:**
566
567以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
568
569| 错误码ID | 错误信息                                                     |
570| -------- | ------------------------------------------------------------ |
571| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
572| 10200060 | Precision limit exceeded.                                    |
573
574**示例:**
575
576```ts
577let a: Decimal = new Decimal(3).pow(-2);
578console.info("test Decimal pow:" + a.toString()); // '0.11111111111111111111'
579```
580
581### exp
582
583exp(): Decimal
584
585返回一个新的Decimal,其值是该Decimal值的自然指数。
586
587使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
588
589**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
590
591**系统能力**:SystemCapability.Utils.Lang
592
593**返回值:**
594
595| 类型                | 返回值                                |
596| ------------------- | ------------------------------------- |
597| [Decimal](#decimal) | 返回自然指数运算后的Decimal对象实例。 |
598
599**错误码:**
600
601以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
602
603| 错误码ID | 错误信息                  |
604| -------- | ------------------------- |
605| 10200060 | Precision limit exceeded. |
606
607**示例:**
608
609```ts
610let a: Decimal = new Decimal(2).exp();
611console.info("test Decimal exp:" + a.toString()); // '7.3890560989306502272'
612```
613
614### log
615
616log(n: Value): Decimal
617
618返回一个值,以n为底的指定的对数运算的Decimal对象。
619
620使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
621
622**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
623
624**系统能力**:SystemCapability.Utils.Lang
625
626**参数:**
627
628| 参数名 | 类型            | 必填 | 说明               |
629| ------ | --------------- | ---- | ------------------ |
630| n      | [Value](#value) | 是   | 对数计算的底数值。 |
631
632**返回值:**
633
634| 类型                | 说明                              |
635| ------------------- | --------------------------------- |
636| [Decimal](#decimal) | 返回对数运算后的Decimal对象实例。 |
637
638**错误码**:
639
640以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
641
642| 错误码ID | 错误信息                                                     |
643| -------- | ------------------------------------------------------------ |
644| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
645| 10200060 | Precision limit exceeded.                                    |
646
647**示例:**
648
649```ts
650let a: Decimal = new Decimal(2).log(256);
651console.info("test Decimal log:" + a.toString()); // '0.125'
652```
653
654### ln
655
656ln(): Decimal
657
658返回一个新的Decimal,其值是此Decimal值的自然对数。
659
660使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
661
662**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
663
664**系统能力**:SystemCapability.Utils.Lang
665
666**返回值:**
667
668| 类型                | 说明                                  |
669| ------------------- | ------------------------------------- |
670| [Decimal](#decimal) | 返回自然对数运算后的Decimal对象实例。 |
671
672**错误码:**
673
674以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
675
676| 错误码ID | 错误信息                  |
677| -------- | ------------------------- |
678| 10200060 | Precision limit exceeded. |
679
680**示例:**
681
682```ts
683let a: Decimal = new Decimal(1.23e+30).ln();
684console.info("test Decimal ln:" + a.toString()); // '69.284566959205696648'
685```
686
687### cos
688
689cos(): Decimal
690
691返回一个新的Decimal,其值是此Decimal的余弦值。
692
693**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
694
695**系统能力**:SystemCapability.Utils.Lang
696
697**返回值:**
698
699| 类型                | 说明                              |
700| ------------------- | --------------------------------- |
701| [Decimal](#decimal) | 返回计算余弦值的Decimal对象实例。 |
702
703**示例:**
704
705```ts
706let a: Decimal = new Decimal(-0.25).cos();
707console.info("test Decimal cos:" + a.toString()); // '0.96891242171064478414'
708```
709
710### sin
711
712sin(): Decimal
713
714返回一个新的Decimal,其值是此Decimal的正弦值。
715
716**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
717
718**系统能力**:SystemCapability.Utils.Lang
719
720**返回值:**
721
722| 类型                | 说明                              |
723| ------------------- | --------------------------------- |
724| [Decimal](#decimal) | 返回计算正弦值的Decimal对象实例。 |
725
726**示例:**
727
728```ts
729let a: Decimal = new Decimal(0.75).sin();
730console.info("test Decimal sin:" + a.toString()); // '0.68163876002333416673'
731```
732
733### tan
734
735tan(): Decimal
736
737返回一个新的Decimal,其值是此Decimal的正切值。
738
739**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
740
741**系统能力**:SystemCapability.Utils.Lang
742
743**返回值:**
744
745| 类型                | 说明                              |
746| ------------------- | --------------------------------- |
747| [Decimal](#decimal) | 返回计算正切值的Decimal对象实例。 |
748
749**示例:**
750
751```ts
752let a: Decimal = new Decimal(0.75).tan();
753console.info("test Decimal tan:" + a.toString()); // '0.93159645994407246117'
754```
755
756### cosh
757
758cosh(): Decimal
759
760返回一个新的Decimal,其值是此Decimal的双曲余弦值。
761
762**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
763
764**系统能力**:SystemCapability.Utils.Lang
765
766**返回值:**
767
768| 类型    | 说明                                |
769| ------- | ----------------------------------- |
770| [Decimal](#decimal) | 返回计算双曲余弦值的Decimal对象实例。 |
771
772**示例:**
773
774```ts
775let a: Decimal = new Decimal(0.5).cosh();
776console.info("test Decimal cosh:" + a.toString()); // '1.1276259652063807852'
777```
778
779### sinh
780
781sinh(): Decimal
782
783返回一个新的Decimal,其值是此Decimal的双曲正弦值。
784
785**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
786
787**系统能力**:SystemCapability.Utils.Lang
788
789**返回值:**
790
791| 类型    | 说明                                |
792| ------- | ----------------------------------- |
793| [Decimal](#decimal) | 返回计算双曲正弦值的Decimal对象实例。 |
794
795**示例:**
796
797```ts
798let a: Decimal = new Decimal(0.5).sinh();
799console.info("test Decimal sinh:" + a.toString()); // '0.52109530549374736162'
800```
801
802### tanh
803
804tanh(): Decimal
805
806返回一个新的Decimal,其值是此Decimal的双曲正切值。
807
808**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
809
810**系统能力**:SystemCapability.Utils.Lang
811
812**返回值:**
813
814| 类型    | 说明                                |
815| ------- | ----------------------------------- |
816| [Decimal](#decimal) | 返回计算双曲正切值的Decimal对象实例。 |
817
818**示例:**
819
820```ts
821let a: Decimal = new Decimal(0.5).tanh();
822console.info("test Decimal tanh:" + a.toString()); // '0.4621171572600097585'
823```
824
825### acos
826
827acos(): Decimal
828
829返回一个新的Decimal,其值是此Decimal的反余弦值。
830
831**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
832
833**系统能力**:SystemCapability.Utils.Lang
834
835**返回值:**
836
837| 类型    | 说明                              |
838| ------- | --------------------------------- |
839| [Decimal](#decimal) | 返回计算反余弦值的Decimal对象实例。 |
840
841**错误码**:
842
843以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
844
845| 错误码ID | 错误信息                  |
846| -------- | ------------------------- |
847| 10200060 | Precision limit exceeded. |
848
849**示例:**
850
851```ts
852let a: Decimal = new Decimal(0.5).acos();
853console.info("test Decimal acos:" + a.toString()); // '1.0471975511965977462'
854```
855
856### asin
857
858asin(): Decimal
859
860返回一个新的Decimal,其值为此Decimal的反正弦值。
861
862**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
863
864**系统能力**:SystemCapability.Utils.Lang
865
866**返回值:**
867
868| 类型    | 说明                              |
869| ------- | --------------------------------- |
870| [Decimal](#decimal) | 返回计算反正弦值的Decimal对象实例。 |
871
872**错误码**:
873
874以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
875
876| 错误码ID | 错误信息                  |
877| -------- | ------------------------- |
878| 10200060 | Precision limit exceeded. |
879
880**示例:**
881
882```ts
883let a: Decimal = new Decimal(0.75).asin();
884console.info("test Decimal asin:" + a.toString()); // '0.84806207898148100805'
885```
886
887### atan
888
889atan(): Decimal
890
891返回一个新的Decimal,其值是此Decimal的反正切值。
892
893**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
894
895**系统能力**:SystemCapability.Utils.Lang
896
897**返回值:**
898
899| 类型    | 说明                              |
900| ------- | --------------------------------- |
901| [Decimal](#decimal) | 返回计算反正切值的Decimal对象实例。 |
902
903**错误码**:
904
905以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
906
907| 错误码ID | 错误信息                  |
908| -------- | ------------------------- |
909| 10200060 | Precision limit exceeded. |
910
911**示例:**
912
913```ts
914let a: Decimal = new Decimal(0.75).atan();
915console.info("test Decimal atan:" + a.toString()); // '0.6435011087932843868'
916```
917
918### acosh
919
920acosh(): Decimal
921
922返回一个新的Decimal,其值是此Decimal值的双曲余弦的倒数。
923
924**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
925
926**系统能力**:SystemCapability.Utils.Lang
927
928**返回值:**
929
930| 类型                | 说明                                        |
931| ------------------- | ------------------------------------------- |
932| [Decimal](#decimal) | 返回计算双曲余弦的倒数值的Decimal对象实例。 |
933
934**错误码**:
935
936以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
937
938| 错误码ID | 错误信息                  |
939| -------- | ------------------------- |
940| 10200060 | Precision limit exceeded. |
941
942**示例:**
943
944```ts
945let a: Decimal = new Decimal(50).acosh();
946console.info("test Decimal acosh:" + a.toString()); // '4.6050701709847571595'
947```
948
949### asinh
950
951asinh(): Decimal
952
953返回一个新的Decimal,其值是此Decimal值的双曲正弦的倒数。
954
955**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
956
957**系统能力**:SystemCapability.Utils.Lang
958
959**返回值:**
960
961| 类型                | 说明                                        |
962| ------------------- | ------------------------------------------- |
963| [Decimal](#decimal) | 返回计算双曲正弦的倒数值的Decimal对象实例。 |
964
965**错误码**:
966
967以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
968
969| 错误码ID | 错误信息                  |
970| -------- | ------------------------- |
971| 10200060 | Precision limit exceeded. |
972
973**示例:**
974
975```ts
976let a: Decimal = new Decimal(50).asinh();
977console.info("test Decimal asinh:" + a.toString()); // '4.6052701709914238266'
978```
979
980### atanh
981
982atanh(): Decimal
983
984返回一个新的Decimal,其值是此Decimal值的双曲正切的倒数。
985
986**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
987
988**系统能力**:SystemCapability.Utils.Lang
989
990**返回值:**
991
992| 类型                | 说明                                        |
993| ------------------- | ------------------------------------------- |
994| [Decimal](#decimal) | 返回计算双曲正切的倒数值的Decimal对象实例。 |
995
996**错误码**:
997
998以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
999
1000| 错误码ID | 错误信息                  |
1001| -------- | ------------------------- |
1002| 10200060 | Precision limit exceeded. |
1003
1004**示例:**
1005
1006```ts
1007let a: Decimal = new Decimal(0.75).atanh();
1008console.info("test Decimal atanh:" + a.toString()); // '0.97295507452765665255'
1009```
1010
1011### comparedTo
1012
1013comparedTo(n: Value): number
1014
1015Decimal的比较方法。
1016
1017**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1018
1019**系统能力**:SystemCapability.Utils.Lang
1020
1021**参数:**
1022
1023| 参数名 | 类型            | 必填 | 说明                  |
1024| ------ | --------------- | ---- | --------------------- |
1025| n      | [Value](#value) | 是   | 待比较的值或Decimal。 |
1026
1027**返回值:**
1028
1029| 类型   | 说明                                                         |
1030| ------ | ------------------------------------------------------------ |
1031| number | 返回该Decimal与n的比较结果:<br>1:该Decimal大于比较值。<br/>-1:该Decimal小于比较值。<br/>0:该Decimal等于比较值。<br/>NaN:该Decimal与比较值有一个值为NaN。 |
1032
1033**错误码**:
1034
1035以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1036
1037| 错误码ID | 错误信息                                                     |
1038| -------- | ------------------------------------------------------------ |
1039| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1040
1041**示例:**
1042
1043```ts
1044let a: Decimal = new Decimal(Infinity);
1045let b: Decimal = new Decimal(5);
1046let c: number = a.comparedTo(b);
1047console.info("test Decimal comparedTo:" + c); // '1'
1048
1049let d: number = b.comparedTo(10.5);
1050console.info("test Decimal comparedTo:" + d); // '-1'
1051```
1052
1053### equals
1054
1055equals(n: Value): boolean
1056
1057返回该Decimal是否等于比较值。
1058
1059**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1060
1061**系统能力**:SystemCapability.Utils.Lang
1062
1063**参数:**
1064
1065| 参数名 | 类型            | 必填 | 说明                  |
1066| ------ | --------------- | ---- | --------------------- |
1067| n      | [Value](#value) | 是   | 待比较的值或Decimal。 |
1068
1069**返回值:**
1070
1071| 类型    | 说明                                             |
1072| ------- | ------------------------------------------------ |
1073| boolean | true表示该Decimal与比较值相等,其余情况为false。 |
1074
1075**错误码**:
1076
1077以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1078
1079| 错误码ID | 错误信息                                                     |
1080| -------- | ------------------------------------------------------------ |
1081| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1082
1083**示例:**
1084
1085```ts
1086let a: Decimal = new Decimal(0);
1087let b: boolean = a.equals('1e-324');
1088console.info("test Decimal equals:" + b); // 'false'
1089```
1090
1091### greaterThan
1092
1093greaterThan(n: Value): boolean
1094
1095返回该Decimal是否大于比较值。
1096
1097**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1098
1099**系统能力**:SystemCapability.Utils.Lang
1100
1101**参数:**
1102
1103| 参数名 | 类型            | 必填 | 说明                  |
1104| ------ | --------------- | ---- | --------------------- |
1105| n      | [Value](#value) | 是   | 待比较的值或Decimal。 |
1106
1107**返回值:**
1108
1109| 类型    | 说明                                           |
1110| ------- | ---------------------------------------------- |
1111| boolean | true表示该Decimal大于比较值,其余情况为false。 |
1112
1113**错误码**:
1114
1115以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1116
1117| 错误码ID | 错误信息                                                     |
1118| -------- | ------------------------------------------------------------ |
1119| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1120
1121**示例:**
1122
1123```ts
1124let a: Decimal = new Decimal(0.1);
1125let b: boolean = a.greaterThan(new Decimal(0.3).sub(0.2)); // 'false'
1126console.info("test Decimal greaterThan:" + b); // 'false'
1127```
1128
1129### greaterThanOrEqualTo
1130
1131greaterThanOrEqualTo(n: Value): boolean
1132
1133返回该Decimal是否大于等于比较值。
1134
1135**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1136
1137**系统能力**:SystemCapability.Utils.Lang
1138
1139**参数:**
1140
1141| 参数名 | 类型            | 必填 | 说明                  |
1142| ------ | --------------- | ---- | --------------------- |
1143| n      | [Value](#value) | 是   | 待比较的值或Decimal。 |
1144
1145**返回值:**
1146
1147| 类型    | 说明                                               |
1148| ------- | -------------------------------------------------- |
1149| boolean | true表示该Decimal大于等于比较值,其余情况为false。 |
1150
1151**错误码**:
1152
1153以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1154
1155| 错误码ID | 错误信息                                                     |
1156| -------- | ------------------------------------------------------------ |
1157| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1158
1159**示例:**
1160
1161```ts
1162let a: Decimal = new Decimal(0.3).sub(0.2);
1163let b: boolean = a.greaterThanOrEqualTo(0.1);
1164console.info("test Decimal greaterThanOrEqualTo:" + b); // 'true'
1165```
1166
1167### lessThan
1168
1169lessThan(n: Value): boolean
1170
1171返回该Decimal是否小于比较值。
1172
1173**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1174
1175**系统能力**:SystemCapability.Utils.Lang
1176
1177**参数:**
1178
1179| 参数名 | 类型            | 必填 | 说明                  |
1180| ------ | --------------- | ---- | --------------------- |
1181| n      | [Value](#value) | 是   | 待比较的值或Decimal。 |
1182
1183**返回值:**
1184
1185| 类型    | 说明                                           |
1186| ------- | ---------------------------------------------- |
1187| boolean | true表示该Decimal小于比较值,其余情况为false。 |
1188
1189**错误码**:
1190
1191以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1192
1193| 错误码ID | 错误信息                                                     |
1194| -------- | ------------------------------------------------------------ |
1195| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1196
1197**示例:**
1198
1199```ts
1200let a: Decimal = new Decimal(0.3).sub(0.2);
1201let b: boolean = a.lessThan(0.1)
1202console.info("test Decimal lessThan:" + b); // 'false'
1203```
1204
1205### lessThanOrEqualTo
1206
1207lessThanOrEqualTo(n: Value): boolean
1208
1209返回该Decimal是否小于等于比较值。
1210
1211**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1212
1213**系统能力**:SystemCapability.Utils.Lang
1214
1215**参数:**
1216
1217| 参数名 | 类型            | 必填 | 说明                  |
1218| ------ | --------------- | ---- | --------------------- |
1219| n      | [Value](#value) | 是   | 待比较的值或Decimal。 |
1220
1221**返回值:**
1222
1223| 类型    | 说明                                               |
1224| ------- | -------------------------------------------------- |
1225| boolean | true表示该Decimal小于等于比较值,其余情况为false。 |
1226
1227**错误码**:
1228
1229以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1230
1231| 错误码ID | 错误信息                                                     |
1232| -------- | ------------------------------------------------------------ |
1233| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1234
1235**示例:**
1236
1237```ts
1238let a: Decimal = new Decimal(0.1);
1239let b: boolean = a.lessThanOrEqualTo(new Decimal(0.3).sub(0.2))
1240console.info("test Decimal lessThanOrEqualTo:" + b); // 'true'
1241```
1242
1243### isFinite
1244
1245isFinite(): boolean
1246
1247返回该Decimal是否为有限值。
1248
1249**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1250
1251**系统能力**:SystemCapability.Utils.Lang
1252
1253**返回值:**
1254
1255| 类型    | 说明                                         |
1256| ------- | -------------------------------------------- |
1257| boolean | true表示该Decimal为有限值,其余情况为false。 |
1258
1259**示例:**
1260
1261```ts
1262let a: Decimal = new Decimal(1);
1263let b: boolean = a.isFinite();
1264console.info("test Decimal isFinite:" + b); // 'true'
1265```
1266
1267### isInteger
1268
1269isInteger(): boolean
1270
1271返回该Decimal是否为整数。
1272
1273**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1274
1275**系统能力**:SystemCapability.Utils.Lang
1276
1277**返回值:**
1278
1279| 类型    | 说明                                       |
1280| ------- | ------------------------------------------ |
1281| boolean | true表示该Decimal为整数,其余情况为false。 |
1282
1283**示例:**
1284
1285```ts
1286let a: Decimal = new Decimal(123.456);
1287let b: boolean = a.isInteger();
1288console.info("test Decimal isInteger:" + b); // 'false'
1289```
1290
1291### isNaN
1292
1293isNaN(): boolean
1294
1295返回该Decimal是否为无效值。
1296
1297**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1298
1299**系统能力**:SystemCapability.Utils.Lang
1300
1301**返回值:**
1302
1303| 类型    | 说明                                      |
1304| ------- | ----------------------------------------- |
1305| boolean | true表示该Decimal为NaN,其余情况为false。 |
1306
1307**示例:**
1308
1309```ts
1310let a: Decimal = new Decimal(NaN);
1311let b: boolean = a.isNaN();
1312console.info("test Decimal isNaN:" + b); // 'true'
1313```
1314
1315### isNegative
1316
1317isNegative(): boolean
1318
1319返回该Decimal是否为负数。
1320
1321**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1322
1323**系统能力**:SystemCapability.Utils.Lang
1324
1325**返回值:**
1326
1327| 类型    | 说明                                       |
1328| ------- | ------------------------------------------ |
1329| boolean | true表示该Decimal为负数,其余情况为false。 |
1330
1331**示例:**
1332
1333```ts
1334let a: Decimal = new Decimal(-5);
1335let b: boolean = a.isNegative();
1336console.info("test Decimal isNegative:" + b); // 'true'
1337
1338let c: Decimal = new Decimal(-0);
1339let d: boolean = a.isNegative();
1340console.info("test Decimal isNegative:" + d); // 'true'
1341```
1342
1343### isPositive
1344
1345isPositive(): boolean
1346
1347返回该Decimal是否为正数。
1348
1349**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1350
1351**系统能力**:SystemCapability.Utils.Lang
1352
1353**返回值:**
1354
1355| 类型    | 说明                                       |
1356| ------- | ------------------------------------------ |
1357| boolean | true表示该Decimal为正数,其余情况为false。 |
1358
1359**示例:**
1360
1361```ts
1362let a: Decimal = new Decimal(5);
1363let b: boolean = a.isPositive();
1364console.info("test Decimal isPositive:" + b); // 'true'
1365
1366let c: Decimal = new Decimal(0);
1367let d: boolean = a.isPositive();
1368console.info("test Decimal isPositive:" + d); // 'true'
1369```
1370
1371### isZero
1372
1373isZero(): boolean
1374
1375返回该Decimal是否为0或是-0。
1376
1377**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1378
1379**系统能力**:SystemCapability.Utils.Lang
1380
1381**返回值:**
1382
1383| 类型    | 说明                                          |
1384| ------- | --------------------------------------------- |
1385| boolean | true表示该Decimal为0或是-0,其余情况为false。 |
1386
1387**示例:**
1388
1389```ts
1390let a: Decimal = new Decimal(0);
1391let b: boolean = a.isZero();
1392console.info("test Decimal isZero:" + b.toString()); // 'true'
1393```
1394
1395### dividedToIntegerBy
1396
1397dividedToIntegerBy(n: Value): Decimal
1398
1399返回该Decimal除以n后获得的整数部分。
1400
1401使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1402
1403**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1404
1405**系统能力**:SystemCapability.Utils.Lang
1406
1407**参数:**
1408
1409| 参数名 | 类型            | 必填 | 说明           |
1410| ------ | --------------- | ---- | -------------- |
1411| n      | [Value](#value) | 是   | 除法的除数值。 |
1412
1413**返回值:**
1414
1415| 类型                | 说明                                                         |
1416| ------------------- | ------------------------------------------------------------ |
1417| [Decimal](#decimal) | 返回一个新的Decimal,其值是将该Decimal的值除以n值的整数部分。 |
1418
1419**错误码**:
1420
1421以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1422
1423| 错误码ID | 错误信息                                                     |
1424| -------- | ------------------------------------------------------------ |
1425| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
1426
1427**示例:**
1428
1429```ts
1430let a: Decimal = new Decimal(5);
1431let b: Decimal = new Decimal(3);
1432let c: Decimal = a.dividedToIntegerBy(b);
1433console.info("test Decimal dividedToIntegerBy:" + c.toString()); // '1'
1434```
1435
1436### negate
1437
1438negate(): Decimal
1439
1440Decimal取反。
1441
1442**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1443
1444**系统能力**:SystemCapability.Utils.Lang
1445
1446**返回值:**
1447
1448| 类型                | 说明                                             |
1449| ------------------- | ------------------------------------------------ |
1450| [Decimal](#decimal) | 返回一个新的Decimal,其值为该Decimal的值乘以-1。 |
1451
1452**示例:**
1453
1454```ts
1455let a: Decimal = new Decimal(1.8);
1456let b: Decimal = a.negate();
1457console.info("test Decimal negate:" + b.toString()); // '-1.8'
1458```
1459
1460### toBinary
1461
1462toBinary(): string
1463
1464转换为二进制表示的字符串。
1465
1466使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1467
1468**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1469
1470**系统能力**:SystemCapability.Utils.Lang
1471
1472**返回值:**
1473
1474| 类型   | 说明                     |
1475| ------ | ------------------------ |
1476| string | 返回二进制表示的字符串。 |
1477
1478**错误码:**
1479
1480以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1481
1482| 错误码ID | 错误信息                                          |
1483| -------- | ------------------------------------------------- |
1484| 10200001 | The value of 'significantDigits' is out of range. |
1485
1486**示例:**
1487
1488```ts
1489let a: Decimal = new Decimal(256);
1490let b: string = a.toBinary();
1491console.info("test Decimal toBinary:" + b); // '0b100000000'
1492```
1493
1494### toBinary
1495
1496toBinary(significantDigits: number): string
1497
1498转换为二进制表示的字符串,可按照significantDigits设置有效数字。
1499
1500使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1501
1502**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1503
1504**系统能力**:SystemCapability.Utils.Lang
1505
1506**参数:**
1507
1508| 参数名            | 类型   | 必填 | 说明                                             |
1509| ----------------- | ------ | ---- | ------------------------------------------------ |
1510| significantDigits | number | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。 |
1511
1512**返回值:**
1513
1514| 类型   | 说明                     |
1515| ------ | ------------------------ |
1516| string | 返回二进制表示的字符串。 |
1517
1518**错误码:**
1519
1520以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1521
1522| 错误码ID | 错误信息                                          |
1523| -------- | ------------------------------------------------- |
1524| 10200001 | The value of 'significantDigits' is out of range. |
1525
1526**示例:**
1527
1528```ts
1529let a: Decimal = new Decimal(256);
1530let b: string = a.toBinary(1);
1531console.info("test Decimal toBinary:" + b); // '0b1p+8'
1532```
1533
1534### toBinary
1535
1536toBinary(significantDigits: number, rounding: Rounding): string
1537
1538转换为二进制表示的字符串,可按照significantDigits设置有效数字,可按照rounding设置舍入模式。
1539
1540**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1541
1542**系统能力**:SystemCapability.Utils.Lang
1543
1544**参数:**
1545
1546| 参数名            | 类型                  | 必填 | 说明                                                      |
1547| ----------------- | --------------------- | ---- | --------------------------------------------------------- |
1548| significantDigits | number                | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。          |
1549| rounding          | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
1550
1551**返回值:**
1552
1553| 类型   | 说明                     |
1554| ------ | ------------------------ |
1555| string | 返回二进制表示的字符串。 |
1556
1557**错误码:**
1558
1559以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1560
1561| 错误码ID | 错误信息                                                     |
1562| -------- | ------------------------------------------------------------ |
1563| 10200001 | The value of 'significantDigits \| rounding' is out of range. |
1564
1565**示例:**
1566
1567```ts
1568let a: Decimal = new Decimal(256);
1569let b: string = a.toBinary(1, Decimal.ROUND_HALF_UP);
1570console.info("test Decimal toBinary:" + b); // '0b1p+8'
1571```
1572
1573### toOctal
1574
1575toOctal(): string
1576
1577转换为八进制表示的字符串。
1578
1579使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1580
1581**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1582
1583**系统能力**:SystemCapability.Utils.Lang
1584
1585**返回值:**
1586
1587| 类型   | 说明                     |
1588| ------ | ------------------------ |
1589| string | 返回八进制表示的字符串。 |
1590
1591**示例:**
1592
1593```ts
1594let a: Decimal = new Decimal(256);
1595let b: string = a.toOctal();
1596console.info("test Decimal toOctal:" + b); // '0o400'
1597```
1598
1599### toOctal
1600
1601toOctal(significantDigits: number): string
1602
1603转换为八进制表示的字符串,可按照significantDigits设置有效数字。
1604
1605使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1606
1607**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1608
1609**系统能力**:SystemCapability.Utils.Lang
1610
1611**参数:**
1612
1613| 参数名            | 类型   | 必填 | 说明                                             |
1614| ----------------- | ------ | ---- | ------------------------------------------------ |
1615| significantDigits | number | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。 |
1616
1617**返回值:**
1618
1619| 类型   | 说明                     |
1620| ------ | ------------------------ |
1621| string | 返回八进制表示的字符串。 |
1622
1623**错误码:**
1624
1625以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1626
1627| 错误码ID | 错误信息                                          |
1628| -------- | ------------------------------------------------- |
1629| 10200001 | The value of 'significantDigits' is out of range. |
1630
1631**示例:**
1632
1633```ts
1634let a: Decimal = new Decimal(256);
1635let b: string = a.toOctal(1);
1636console.info("test Decimal toOctal:" + b); // '0o1p+8'
1637```
1638
1639### toOctal
1640
1641toOctal(significantDigits: number, rounding: Rounding): string
1642
1643转换为八进制表示的字符串,可按照significantDigits设置有效数字,可按照rounding设置舍入模式。
1644
1645**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1646
1647**系统能力**:SystemCapability.Utils.Lang
1648
1649**参数:**
1650
1651| 参数名            | 类型                  | 必填 | 说明                                                      |
1652| ----------------- | --------------------- | ---- | --------------------------------------------------------- |
1653| significantDigits | number                | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。          |
1654| rounding          | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
1655
1656**返回值:**
1657
1658| 类型   | 说明                     |
1659| ------ | ------------------------ |
1660| string | 返回八进制表示的字符串。 |
1661
1662**错误码:**
1663
1664以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1665
1666| 错误码ID | 错误信息                                                     |
1667| -------- | ------------------------------------------------------------ |
1668| 10200001 | The value of 'significantDigits \| rounding' is out of range. |
1669
1670**示例:**
1671
1672```ts
1673let a: Decimal = new Decimal(256);
1674let b: string = a.toOctal(1, Decimal.ROUND_HALF_UP);
1675console.info("test Decimal toOctal:" + b); // '0o1p+8'
1676```
1677
1678### toHexadecimal
1679
1680toHexadecimal(): string
1681
1682转换为十六进制表示的字符串。
1683
1684使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1685
1686**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1687
1688**系统能力**:SystemCapability.Utils.Lang
1689
1690**返回值:**
1691
1692| 类型   | 说明                       |
1693| ------ | -------------------------- |
1694| string | 返回十六进制表示的字符串。 |
1695
1696**示例:**
1697
1698```ts
1699let a: Decimal = new Decimal(256);
1700let b: string = a.toHexadecimal();
1701console.info("test Decimal toHexadecimal:" + b); // '0x100'
1702```
1703
1704### toHexadecimal
1705
1706toHexadecimal(significantDigits: number): string
1707
1708转换为十六进制表示的字符串,可按照significantDigits设置有效数字。
1709
1710使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1711
1712**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1713
1714**系统能力**:SystemCapability.Utils.Lang
1715
1716**参数:**
1717
1718| 参数名            | 类型   | 必填 | 说明                                             |
1719| ----------------- | ------ | ---- | ------------------------------------------------ |
1720| significantDigits | number | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。 |
1721
1722**返回值:**
1723
1724| 类型   | 说明                       |
1725| ------ | -------------------------- |
1726| string | 返回十六进制表示的字符串。 |
1727
1728**错误码:**
1729
1730以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1731
1732| 错误码ID | 错误信息                                          |
1733| -------- | ------------------------------------------------- |
1734| 10200001 | The value of 'significantDigits' is out of range. |
1735
1736**示例:**
1737
1738```ts
1739let a: Decimal = new Decimal(256);
1740let b: string = a.toHexadecimal(1);
1741console.info("test Decimal toHexadecimal:" + b); // '0x1p+8'
1742```
1743
1744### toHexadecimal
1745
1746toHexadecimal(significantDigits: number, rounding: Rounding): string
1747
1748转换为十六进制表示的字符串,可按照significantDigits设置有效数字,可按照rounding设置舍入模式。
1749
1750**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1751
1752**系统能力**:SystemCapability.Utils.Lang
1753
1754**参数:**
1755
1756| 参数名            | 类型                  | 必填 | 说明                                                      |
1757| ----------------- | --------------------- | ---- | --------------------------------------------------------- |
1758| significantDigits | number                | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。          |
1759| rounding          | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
1760
1761**返回值:**
1762
1763| 类型   | 说明                       |
1764| ------ | -------------------------- |
1765| string | 返回十六进制表示的字符串。 |
1766
1767**错误码:**
1768
1769以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1770
1771| 错误码ID | 错误信息                                                     |
1772| -------- | ------------------------------------------------------------ |
1773| 10200001 | The value of 'significantDigits \| rounding' is out of range. |
1774
1775**示例:**
1776
1777```ts
1778let a: Decimal = new Decimal(256);
1779let b: string = a.toHexadecimal(1, Decimal.ROUND_HALF_UP);
1780console.info("test Decimal toHexadecimal:" + b); // '0x1p+8'
1781```
1782
1783### toDecimalPlaces
1784
1785toDecimalPlaces(): Decimal
1786
1787返回一个保留小数点后指定位数的Decimal对象,不进行小数的取舍。
1788
1789**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1790
1791**系统能力**:SystemCapability.Utils.Lang
1792
1793**返回值:**
1794
1795| 类型                | 说明                                        |
1796| ------------------- | ------------------------------------------- |
1797| [Decimal](#decimal) | 返回一个新的Decimal,保留小数点后指定位数。 |
1798
1799**示例:**
1800
1801```ts
1802let a: Decimal = new Decimal(12.34567);
1803let b: Decimal = a.toDecimalPlaces();
1804console.info("test Decimal toDecimalPlaces:" + b.toString()); // '12.34567'
1805```
1806
1807### toDecimalPlaces
1808
1809toDecimalPlaces(decimalPlaces: number): Decimal
1810
1811返回一个保留小数点后指定位数的Decimal对象,可按照decimalPlaces设置小数位数。
1812
1813使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1814
1815**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1816
1817**系统能力**:SystemCapability.Utils.Lang
1818
1819**参数:**
1820
1821| 参数名        | 类型   | 必填 | 说明                                                     |
1822| ------------- | ------ | ---- | -------------------------------------------------------- |
1823| decimalPlaces | number | 是   | 转换时保留的小数点后有效位数,取值范围为[0, 1e9]的整数。 |
1824
1825**返回值:**
1826
1827| 类型                | 说明                                        |
1828| ------------------- | ------------------------------------------- |
1829| [Decimal](#decimal) | 返回一个新的Decimal,保留小数点后指定位数。 |
1830
1831**错误码:**
1832
1833以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1834
1835| 错误码ID | 错误信息                                      |
1836| -------- | --------------------------------------------- |
1837| 10200001 | The value of 'decimalPlaces' is out of range. |
1838
1839**示例:**
1840
1841```ts
1842let a: Decimal = new Decimal(9876.54321);
1843let b: Decimal = a.toDecimalPlaces(3);
1844console.info("test Decimal toDecimalPlaces:" + b.toString()); // '9876.543'
1845```
1846
1847### toDecimalPlaces
1848
1849toDecimalPlaces(decimalPlaces: number, rounding: Rounding): Decimal
1850
1851返回一个保留小数点后指定位数的Decimal对象,可按照decimalPlaces设置小数位数,可按照rounding设置舍入模式。
1852
1853**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1854
1855**系统能力**:SystemCapability.Utils.Lang
1856
1857**参数:**
1858
1859| 参数名        | 类型                  | 必填 | 说明                                                      |
1860| ------------- | --------------------- | ---- | --------------------------------------------------------- |
1861| decimalPlaces | number                | 是   | 转换时保留的小数点后有效位数,取值范围为[0, 1e9]的整数。  |
1862| rounding      | [Rounding](#rounding) | 是   | 转换时使用的舍入模式。取值范围参考[Rounding](#rounding)。 |
1863
1864**返回值:**
1865
1866| 类型                | 说明                                        |
1867| ------------------- | ------------------------------------------- |
1868| [Decimal](#decimal) | 返回一个新的Decimal,保留小数点后指定位数。 |
1869
1870**错误码:**
1871
1872以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1873
1874| 错误码ID | 错误信息                                                  |
1875| -------- | --------------------------------------------------------- |
1876| 10200001 | The value of 'decimalPlaces \| rounding' is out of range. |
1877
1878**示例:**
1879
1880```ts
1881let a: Decimal = new Decimal(9876.54321);
1882let b: Decimal = a.toDecimalPlaces(1, 0);
1883console.info("test Decimal toDecimalPlaces:" + b.toString()); // '9876.6'
1884b = a.toDecimalPlaces(1, Decimal.ROUND_DOWN) // b:'9876.5'
1885```
1886
1887### toExponential
1888
1889toExponential(): string
1890
1891转换为按照指数表示法显示的字符串,不进行小数的取舍。
1892
1893**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1894
1895**系统能力**:SystemCapability.Utils.Lang
1896
1897**返回值:**
1898
1899| 类型   | 说明                             |
1900| ------ | -------------------------------- |
1901| string | 返回按照指数表示法显示的字符串。 |
1902
1903**示例:**
1904
1905```ts
1906let a: Decimal = new Decimal(45.6);
1907let b: string = a.toExponential();
1908console.info("test Decimal toExponential:" + b); // '4.56e+1'
1909```
1910
1911### toExponential
1912
1913toExponential(decimalPlaces: number): string
1914
1915转换为按照指数表示法显示的字符串,可按照decimalPlaces设置小数位数。
1916
1917使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
1918
1919**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1920
1921**系统能力**:SystemCapability.Utils.Lang
1922
1923**参数:**
1924
1925| 参数名        | 类型   | 必填 | 说明                                                     |
1926| ------------- | ------ | ---- | -------------------------------------------------------- |
1927| decimalPlaces | number | 是   | 转换时保留的小数点后有效位数,取值范围为[0, 1e9]的整数。 |
1928
1929**返回值:**
1930
1931| 类型   | 说明                             |
1932| ------ | -------------------------------- |
1933| string | 返回按照指数表示法显示的字符串。 |
1934
1935**错误码:**
1936
1937以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1938
1939| 错误码ID | 错误信息                                      |
1940| -------- | --------------------------------------------- |
1941| 10200001 | The value of 'decimalPlaces' is out of range. |
1942
1943**示例:**
1944
1945```ts
1946let a: Decimal = new Decimal(45.6);
1947let b: string = a.toExponential(0);
1948console.info("test Decimal toExponential:" + b); // '5e+1'
1949b = a.toExponential(1) // b:'4.6e+1'
1950b = a.toExponential(3) // b:'4.560e+1'
1951```
1952
1953### toExponential
1954
1955toExponential(decimalPlaces: number, rounding: Rounding): string
1956
1957转换为按照指数表示法显示的字符串,可按照decimalPlaces设置小数位数,可按照rounding设置舍入模式。
1958
1959**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1960
1961**系统能力**:SystemCapability.Utils.Lang
1962
1963**参数:**
1964
1965| 参数名        | 类型                  | 必填 | 说明                                                      |
1966| ------------- | --------------------- | ---- | --------------------------------------------------------- |
1967| decimalPlaces | number                | 是   | 转换时保留的小数点后有效位数,取值范围为[0, 1e9]的整数。  |
1968| rounding      | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
1969
1970**返回值:**
1971
1972| 类型   | 说明                             |
1973| ------ | -------------------------------- |
1974| string | 返回按照指数表示法显示的字符串。 |
1975
1976**错误码:**
1977
1978以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
1979
1980| 错误码ID | 错误信息                                                  |
1981| -------- | --------------------------------------------------------- |
1982| 10200001 | The value of 'decimalPlaces \| rounding' is out of range. |
1983
1984**示例:**
1985
1986```ts
1987let a: Decimal = new Decimal(45.6);
1988let b = a.toExponential(1, Decimal.ROUND_DOWN)
1989console.info("test Decimal toExponential:" + b); // '4.5e+1'
1990```
1991
1992### toFixed
1993
1994toFixed(): string
1995
1996转换为十进制定点模式表示的字符串,不进行小数的取舍。
1997
1998**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
1999
2000**系统能力**:SystemCapability.Utils.Lang
2001
2002**返回值:**
2003
2004| 类型                | 说明                                             |
2005| ------------------- | ------------------------------------------------ |
2006| [Decimal](#decimal) | 返回按照正常模式(十进制定点模式)表示的字符串。 |
2007
2008**示例:**
2009
2010```ts
2011let a: Decimal = new Decimal(3.456);
2012let b: string = a.toFixed();
2013console.info("test Decimal toFixed:" + b); // '3.456'
2014```
2015
2016### toFixed
2017
2018toFixed(decimalPlaces: number): string
2019
2020转换为十进制定点模式表示的字符串,可按照decimalPlaces设置小数位数。
2021
2022使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2023
2024**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2025
2026**系统能力**:SystemCapability.Utils.Lang
2027
2028**参数:**
2029
2030| 参数名        | 类型   | 必填 | 说明                                                     |
2031| ------------- | ------ | ---- | -------------------------------------------------------- |
2032| decimalPlaces | number | 是   | 转换时保留的小数点后有效位数,取值范围为[0, 1e9]的整数。 |
2033
2034**返回值:**
2035
2036| 类型                | 说明                                             |
2037| ------------------- | ------------------------------------------------ |
2038| [Decimal](#decimal) | 返回按照正常模式(十进制定点模式)表示的字符串。 |
2039
2040**错误码:**
2041
2042以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2043
2044| 错误码ID | 错误信息                                      |
2045| -------- | --------------------------------------------- |
2046| 10200001 | The value of 'decimalPlaces' is out of range. |
2047
2048**示例:**
2049
2050```ts
2051let a: Decimal = new Decimal(3.456);
2052let b: string = a.toFixed(0)
2053console.info("test Decimal toFixed:" + b); // '3'
2054b = a.toFixed(2) // b:'3.46'
2055b = a.toFixed(5) // b:'3.45600'
2056```
2057
2058### toFixed
2059
2060toFixed(decimalPlaces: number, rounding: Rounding): string
2061
2062转换为十进制定点模式表示的字符串,可按照decimalPlaces设置小数位数,可按照rounding设置舍入模式。
2063
2064**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2065
2066**系统能力**:SystemCapability.Utils.Lang
2067
2068**参数:**
2069
2070| 参数名        | 类型                  | 必填 | 说明                                                      |
2071| ------------- | --------------------- | ---- | --------------------------------------------------------- |
2072| decimalPlaces | number                | 是   | 转换时保留的小数点后有效位数,取值范围为[0, 1e9]的整数。  |
2073| rounding      | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
2074
2075**返回值:**
2076
2077| 类型                | 说明                                             |
2078| ------------------- | ------------------------------------------------ |
2079| [Decimal](#decimal) | 返回按照正常模式(十进制定点模式)表示的字符串。 |
2080
2081**错误码:**
2082
2083以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2084
2085| 错误码ID | 错误信息                                                  |
2086| -------- | --------------------------------------------------------- |
2087| 10200001 | The value of 'decimalPlaces \| rounding' is out of range. |
2088
2089**示例:**
2090
2091```ts
2092let a: Decimal = new Decimal(3.456);
2093let b: string = a.toFixed(2, Decimal.ROUND_DOWN);
2094console.info("test Decimal toFixed:" + b); // b:'3.45'
2095```
2096
2097### toFraction
2098
2099toFraction(): Decimal[]
2100
2101转换为分数表示的数。
2102
2103**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2104
2105**系统能力**:SystemCapability.Utils.Lang
2106
2107**返回值:**
2108
2109| 类型                  | 说明                                                         |
2110| --------------------- | ------------------------------------------------------------ |
2111| [Decimal](#decimal)[] | 返回一个Decimal数组,该数组长度固定为2,其值表示为具有整数分子和整数分母的简单分数。且分子在前,分母在后。 |
2112
2113**示例:**
2114
2115```ts
2116let a: Decimal = new Decimal(1.75);
2117let b: Decimal[] = a.toFraction();
2118console.info("test Decimal toFraction:" + b.toString()); // '7,4'
2119```
2120
2121### toFraction
2122
2123toFraction(max_denominator: Value): Decimal[]
2124
2125转换为分数表示的数,可以通过max_denominator设置最大分母值。
2126
2127**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2128
2129**系统能力**:SystemCapability.Utils.Lang
2130
2131**参数:**
2132
2133| 参数名          | 类型            | 必填 | 说明                     |
2134| --------------- | --------------- | ---- | ------------------------ |
2135| max_denominator | [Value](#value) | 是   | 分母的最大值。包含该值。 |
2136
2137**返回值:**
2138
2139| 类型                  | 说明                                                         |
2140| --------------------- | ------------------------------------------------------------ |
2141| [Decimal](#decimal)[] | 返回一个Decimal数组,该数组长度固定为2,其值表示为具有整数分子和整数分母的简单分数。且分子在前,分母在后。 |
2142
2143**错误码:**
2144
2145以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2146
2147| 错误码ID | 错误信息                                                     |
2148| -------- | ------------------------------------------------------------ |
2149| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2150
2151**示例:**
2152
2153```ts
2154let pi: Decimal = new Decimal('3.14159265358')
2155let b = pi.toFraction() // b:'157079632679,50000000000'
2156b = pi.toFraction(100000) // b:'312689, 99532'
2157b = pi.toFraction(10000) // b:'355, 113'
2158b = pi.toFraction(100) // b:'311, 99'
2159b = pi.toFraction(10) // b:'22, 7'
2160b = pi.toFraction(1) // b:'3, 1'
2161```
2162
2163### toNearest
2164
2165toNearest(n: Value): Decimal
2166
2167返回一个新的Decimal,该Decimal为指定值乘以一个倍数后与原Decimal最接近的值。
2168
2169**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2170
2171**系统能力**:SystemCapability.Utils.Lang
2172
2173**参数:**
2174
2175| 参数名 | 类型            | 必填 | 说明           |
2176| ------ | --------------- | ---- | -------------- |
2177| n      | [Value](#value) | 是   | 参考的指定值。 |
2178
2179**返回值:**
2180
2181| 类型    | 说明                                        |
2182| ------- | ------------------------------------------- |
2183| Decimal | 返回一个新的Decimal,指定值最接近的倍数值。 |
2184
2185**错误码:**
2186
2187以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2188
2189| 错误码ID | 错误信息                                 |
2190| -------- | ---------------------------------------- |
2191| 10200001 | The value of 'rounding' is out of range. |
2192
2193**示例:**
2194
2195```ts
2196let a: Decimal = new Decimal(1.39);
2197let b: Decimal = a.toNearest(0.25);
2198console.info("test Decimal toNearest:" + b.toString()); // '1.5'
2199```
2200
2201### toNearest
2202
2203toNearest(n: Value, rounding: Rounding): Decimal
2204
2205返回一个新的Decimal,该Decimal为指定值乘以一个倍数后与原Decimal最接近的值,可按照rounding设置舍入模式。
2206
2207**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2208
2209**系统能力**:SystemCapability.Utils.Lang
2210
2211**参数:**
2212
2213| 参数名   | 类型                  | 必填 | 说明                                                      |
2214| -------- | --------------------- | ---- | --------------------------------------------------------- |
2215| n        | [Value](#value)       | 是   | 参考的指定值。                                            |
2216| rounding | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
2217
2218**返回值:**
2219
2220| 类型                | 说明                                        |
2221| ------------------- | ------------------------------------------- |
2222| [Decimal](#decimal) | 返回一个新的Decimal,指定值最接近的倍数值。 |
2223
2224**错误码:**
2225
2226以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2227
2228| 错误码ID | 错误信息                                 |
2229| -------- | ---------------------------------------- |
2230| 10200001 | The value of 'rounding' is out of range. |
2231
2232**示例:**
2233
2234```ts
2235let a: Decimal = new Decimal(9.499)
2236let b = a.toNearest(0.5, Decimal.ROUND_UP) // b:'9.5'
2237b = a.toNearest(0.5, Decimal.ROUND_DOWN) // b:'9'
2238```
2239
2240### toPrecision
2241
2242toPrecision(): string
2243
2244转换为字符串。
2245
2246使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2247
2248**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2249
2250**系统能力**:SystemCapability.Utils.Lang
2251
2252**返回值:**
2253
2254| 类型   | 说明                              |
2255| ------ | --------------------------------- |
2256| string | 返回一个表示Decimal对象的字符串。 |
2257
2258**示例:**
2259
2260```ts
2261let a: Decimal = new Decimal(45.6);
2262let b: string = a.toPrecision();
2263console.info("test Decimal toPrecision:" + b); // '45.6'
2264```
2265
2266### toPrecision
2267
2268toPrecision(significantDigits: number): string
2269
2270转换为字符串,可按照significantDigits设置有效数字。
2271
2272使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2273
2274**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2275
2276**系统能力**:SystemCapability.Utils.Lang
2277
2278**参数:**
2279
2280| 参数名            | 类型   | 必填 | 说明                   |
2281| ----------------- | ------ | ---- | ---------------------- |
2282| significantDigits | number | 是   | 转换时保留的有效数字。 |
2283
2284**返回值:**
2285
2286| 类型   | 说明                              |
2287| ------ | --------------------------------- |
2288| string | 返回一个表示Decimal对象的字符串。 |
2289
2290**错误码:**
2291
2292以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2293
2294| 错误码ID | 错误信息                                          |
2295| -------- | ------------------------------------------------- |
2296| 10200001 | The value of 'significantDigits' is out of range. |
2297
2298**示例:**
2299
2300```ts
2301let a: Decimal = new Decimal(45.6);
2302let b: string = a.toPrecision(1);
2303console.info("test Decimal toPrecision:" + b); // '5e+1'
2304b = a.toPrecision(5); // b:'45.600'
2305```
2306
2307### toPrecision
2308
2309toPrecision(significantDigits: number, rounding: Rounding): string
2310
2311转换为字符串,可按照significantDigits设置有效数字,可按照rounding设置舍入模式。
2312
2313**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2314
2315**系统能力**:SystemCapability.Utils.Lang
2316
2317**参数:**
2318
2319| 参数名            | 类型                  | 必填 | 说明                                                      |
2320| ----------------- | --------------------- | ---- | --------------------------------------------------------- |
2321| significantDigits | number                | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。          |
2322| rounding          | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
2323
2324**返回值:**
2325
2326| 类型   | 说明                              |
2327| ------ | --------------------------------- |
2328| string | 返回一个表示Decimal对象的字符串。 |
2329
2330**错误码:**
2331
2332以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2333
2334| 错误码ID | 错误信息                                                     |
2335| -------- | ------------------------------------------------------------ |
2336| 10200001 | The value of 'significantDigits \|  rounding' is out of range. |
2337
2338**示例:**
2339
2340```ts
2341let a: Decimal = new Decimal(45.6);
2342let b: string = a.toPrecision(2, Decimal.ROUND_UP) // b:'46'
2343b = a.toPrecision(2, Decimal.ROUND_DOWN) // b:'45'
2344```
2345
2346### toSignificantDigits
2347
2348toSignificantDigits(): Decimal
2349
2350返回一个按照保留有效数字的转换的Decimal对象。
2351
2352使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2353
2354**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2355
2356**系统能力**:SystemCapability.Utils.Lang
2357
2358**返回值:**
2359
2360| 类型    | 说明                                    |
2361| ------- | --------------------------------------- |
2362| [Decimal](#decimal) | 返回一个保留有效数字后的Decimal对象实例。 |
2363
2364**示例:**
2365
2366```ts
2367let a: Decimal = new Decimal(987.654321);
2368let b: Decimal = a.toSignificantDigits();
2369console.info("test Decimal toSignificantDigits:" + b.toString()); // '987.654321'
2370```
2371
2372### toSignificantDigits
2373
2374toSignificantDigits(significantDigits: number): Decimal
2375
2376返回一个按照保留有效数字的转换的Decimal对象,可按照significantDigits设置有效数字。
2377
2378使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2379
2380**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2381
2382**系统能力**:SystemCapability.Utils.Lang
2383
2384**参数:**
2385
2386| 参数名            | 类型   | 必填 | 说明                   |
2387| ----------------- | ------ | ---- | ---------------------- |
2388| significantDigits | number | 是   | 转换时保留的有效数字。 |
2389
2390**返回值:**
2391
2392| 类型                | 说明                                      |
2393| ------------------- | ----------------------------------------- |
2394| [Decimal](#decimal) | 返回一个保留有效数字后的Decimal对象实例。 |
2395
2396**错误码:**
2397
2398以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2399
2400| 错误码ID | 错误信息                                          |
2401| -------- | ------------------------------------------------- |
2402| 10200001 | The value of 'significantDigits' is out of range. |
2403
2404**示例:**
2405
2406```ts
2407let a: Decimal = new Decimal(987.654321);
2408let b: Decimal = a.toSignificantDigits(6);
2409console.info("test Decimal toSignificantDigits:" + b.toString()); // '9876.54'
2410```
2411
2412### toSignificantDigits
2413
2414toSignificantDigits(significantDigits: number, rounding: Rounding): Decimal
2415
2416返回一个按照保留有效数字的转换的Decimal对象,可按照significantDigits设置有效数字,可按照rounding设置舍入模式。
2417
2418**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2419
2420**系统能力**:SystemCapability.Utils.Lang
2421
2422**参数:**
2423
2424| 参数名            | 类型                  | 必填 | 说明                                                      |
2425| ----------------- | --------------------- | ---- | --------------------------------------------------------- |
2426| significantDigits | number                | 是   | 转换时保留的有效数字,取值范围为[1, 1e9]的整数。          |
2427| rounding          | [Rounding](#rounding) | 是   | 转换时使用的舍入模式,取值范围参考[Rounding](#rounding)。 |
2428
2429**返回值:**
2430
2431| 类型                | 说明                                      |
2432| ------------------- | ----------------------------------------- |
2433| [Decimal](#decimal) | 返回一个保留有效数字后的Decimal对象实例。 |
2434
2435**错误码:**
2436
2437以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2438
2439| 错误码ID | 错误信息                                                     |
2440| -------- | ------------------------------------------------------------ |
2441| 10200001 | The value of 'significantDigits \|  rounding' is out of range. |
2442
2443**示例:**
2444
2445```ts
2446let a: Decimal = new Decimal(987.654321);
2447let b: Decimal = a.toSignificantDigits(6, Decimal.ROUND_UP);
2448console.info("test Decimal toSignificantDigits:" + b.toString()); // '9876.55'
2449```
2450
2451### toNumber
2452
2453toNumber(): number
2454
2455转换为number类型的值。
2456
2457**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2458
2459**系统能力**:SystemCapability.Utils.Lang
2460
2461**返回值:**
2462
2463| 类型   | 说明                            |
2464| ------ | ------------------------------- |
2465| number | 返回一个表示Decimal的number值。 |
2466
2467**示例:**
2468
2469```ts
2470let a: Decimal = new Decimal(456.789);
2471let b: number = a.toNumber();
2472console.info("test Decimal toNumber:" + b.toString()); // '456.789'
2473```
2474
2475### toString
2476
2477toString(): string
2478
2479返回一个字符串,表示此 Decimal 的值,如果此 Decimal 的正指数等于或大于[toExpPos](#decimalconfig),或负指数等于或小于[toExpNeg](#decimalconfig),则将返回指数表示法。
2480
2481**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2482
2483**系统能力**:SystemCapability.Utils.Lang
2484
2485**返回值:**
2486
2487| 类型   | 说明                          |
2488| ------ | ----------------------------- |
2489| string | 返回一个表示Decimal的字符串。 |
2490
2491**示例:**
2492
2493```ts
2494let a: Decimal = new Decimal(750000);
2495let b: string = a.toString();
2496console.info("test Decimal toString:" + b); // '750000'
2497
2498Decimal.set({ toExpPos: 5 })
2499b = a.toString() // b:'7.5e+5'
2500
2501let c: Decimal = new Decimal(0.000000123)
2502console.info("test Decimal toString:" + c.toString()); // '0.000000123'
2503
2504Decimal.set({ toExpNeg: -7 })
2505b = c.toString() // b:'1.23e-7'
2506```
2507
2508### valueOf
2509
2510valueOf(): string
2511
2512返回一个字符串,表示此 Decimal 的值,负零包含减号。
2513
2514**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2515
2516**系统能力**:SystemCapability.Utils.Lang
2517
2518**返回值:**
2519
2520| 类型   | 说明                          |
2521| ------ | ----------------------------- |
2522| string | 返回一个表示Decimal的字符串。 |
2523
2524**示例:**
2525
2526```ts
2527let a: Decimal = new Decimal(-0);
2528let b: string = a.valueOf();
2529console.info("test Decimal valueOf:" + b); // '-0'
2530```
2531
2532### decimalPlaces
2533
2534decimalPlaces(): number
2535
2536返回Decimal对象的小数位数。
2537
2538**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2539
2540**系统能力**:SystemCapability.Utils.Lang
2541
2542**返回值:**
2543
2544| 类型   | 说明                        |
2545| ------ | --------------------------- |
2546| number | 返回Decimal对象的小数位数。 |
2547
2548**示例:**
2549
2550```ts
2551let a: Decimal = new Decimal(1.234);
2552let b: number = a.decimalPlaces();
2553console.info("test Decimal decimalPlaces:" + b); // '3'
2554```
2555
2556### precision
2557
2558precision(): number
2559
2560返回Decimal对象的有效数字位数。
2561
2562**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2563
2564**系统能力**:SystemCapability.Utils.Lang
2565
2566**返回值:**
2567
2568| 类型   | 说明                        |
2569| ------ | --------------------------- |
2570| number | 返回Decimal对象的有效位数。 |
2571
2572**示例:**
2573
2574```ts
2575let a: Decimal = new Decimal(1.234);
2576let b: number = a.precision();
2577console.info("test Decimal precision:" + b); // '4'
2578```
2579
2580### precision
2581
2582precision(includeZeros: boolean | number): number
2583
2584返回Decimal对象的有效数字位数,通过includeZeros判断是否计算整数部分的尾随零。
2585
2586**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2587
2588**系统能力**:SystemCapability.Utils.Lang
2589
2590**参数:**
2591
2592| 参数名       | 类型    | 必填 | 说明                                                         |
2593| ------------ | ------- | ---- | ------------------------------------------------------------ |
2594| includeZeros | boolean | 是   | 是否计算整数部分尾随零。true表示计算整数部分尾随零,false表示不计算整数部分尾随零。 |
2595
2596**返回值:**
2597
2598| 类型   | 说明                        |
2599| ------ | --------------------------- |
2600| number | 返回Decimal对象的有效位数。 |
2601
2602**错误码:**
2603
2604以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
2605
2606| 错误码ID | 错误信息                                   |
2607| -------- | ------------------------------------------ |
2608| 10200001 | The value of includeZeros is out of range. |
2609
2610**示例:**
2611
2612```ts
2613let a: Decimal = new Decimal(987000);
2614let b: number = a.precision();
2615console.info("test Decimal precision:" + b); // '3'
2616b = a.precision(true) // b:'6'
2617```
2618
2619### abs
2620
2621static abs(n: Value): Decimal
2622
2623返回一个新的Decimal对象,Decimal的值为参数n的绝对值。
2624
2625**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2626
2627**系统能力**:SystemCapability.Utils.Lang
2628
2629**参数:**
2630
2631| 参数名 | 类型            | 必填 | 说明             |
2632| ------ | --------------- | ---- | ---------------- |
2633| n      | [Value](#value) | 是   | 取绝对值的参数。 |
2634
2635**返回值:**
2636
2637| 类型                | 说明                                   |
2638| ------------------- | -------------------------------------- |
2639| [Decimal](#decimal) | 返回一个值为参数n的绝对值的Decimal。 |
2640
2641**错误码:**
2642
2643以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2644
2645| 错误码ID | 错误信息                                                     |
2646| -------- | ------------------------------------------------------------ |
2647| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2648
2649**示例:**
2650
2651```ts
2652let a: Decimal = Decimal.abs(-0.5);
2653console.info("test Decimal abs:" + a.toString()); // '0.5'
2654```
2655
2656### floor
2657
2658static floor(n: Value): Decimal
2659
2660返回一个新的Decimal对象,其值为该Decimal向负无穷方向舍入得到的结果。
2661
2662**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2663
2664**系统能力**:SystemCapability.Utils.Lang
2665
2666**参数:**
2667
2668| 参数名 | 类型            | 必填 | 说明           |
2669| ------ | --------------- | ---- | -------------- |
2670| n      | [Value](#value) | 是   | 需要舍入的值。 |
2671
2672**返回值:**
2673
2674| 类型                | 说明                            |
2675| ------------------- | ------------------------------- |
2676| [Decimal](#decimal) | 返回舍入之后的Decimal对象实例。 |
2677
2678**错误码:**
2679
2680以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2681
2682| 错误码ID | 错误信息                                                     |
2683| -------- | ------------------------------------------------------------ |
2684| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2685
2686**示例:**
2687
2688```ts
2689let a: Decimal = Decimal.floor(1.8);
2690console.info("test Decimal floor:" + a.toString()); // '1'
2691```
2692
2693### ceil
2694
2695static ceil(n: Value): Decimal
2696
2697返回一个新的Decimal对象,其值为该Decimal向正无穷方向舍入得到的结果。
2698
2699**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2700
2701**系统能力**:SystemCapability.Utils.Lang
2702
2703**参数:**
2704
2705| 参数名 | 类型            | 必填 | 说明           |
2706| ------ | --------------- | ---- | -------------- |
2707| n      | [Value](#value) | 是   | 需要舍入的值。 |
2708
2709**返回值:**
2710
2711| 类型                | 说明                            |
2712| ------------------- | ------------------------------- |
2713| [Decimal](#decimal) | 返回舍入之后的Decimal对象实例。 |
2714
2715**错误码:**
2716
2717以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2718
2719| 错误码ID | 错误信息                                                     |
2720| -------- | ------------------------------------------------------------ |
2721| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2722
2723**示例:**
2724
2725```ts
2726let a: Decimal = Decimal.ceil(1.8);
2727console.info("test Decimal ceil:" + a.toString()); // '2'
2728```
2729
2730### trunc
2731
2732static trunc(n: Value): Decimal
2733
2734返回一个新的Decimal,其值是将此Decimal截断为整数部分。
2735
2736**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2737
2738**系统能力**:SystemCapability.Utils.Lang
2739
2740**参数:**
2741
2742| 参数名 | 类型            | 必填 | 说明           |
2743| ------ | --------------- | ---- | -------------- |
2744| n      | [Value](#value) | 是   | 需要截断的值。 |
2745
2746**返回值:**
2747
2748| 类型                | 说明                            |
2749| ------------------- | ------------------------------- |
2750| [Decimal](#decimal) | 返回截断之后的Decimal对象实例。 |
2751
2752**错误码:**
2753
2754以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2755
2756| 错误码ID | 错误信息                                                     |
2757| -------- | ------------------------------------------------------------ |
2758| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2759
2760**示例:**
2761
2762```ts
2763let a: Decimal = Decimal.trunc(2.5);
2764console.info("test Decimal trunc:" + a.toString()); // '2'
2765```
2766
2767### clamp
2768
2769static clamp(n: Value, min: Value, max: Value): Decimal
2770
2771返回一个值为将该Decimal的值限制在min到max范围内的Decimal对象。
2772
2773**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2774
2775**系统能力**:SystemCapability.Utils.Lang
2776
2777**参数:**
2778
2779| 参数名 | 类型            | 必填 | 说明                     |
2780| ------ | --------------- | ---- | ------------------------ |
2781| n      | [Value](#value) | 是   | 需要被限制的值。         |
2782| min    | [Value](#value) | 是   | 限制的最小值。包含该值。 |
2783| max    | [Value](#value) | 是   | 限制的最大值。包含该值。 |
2784
2785**返回值:**
2786
2787| 类型                | 说明                            |
2788| ------------------- | ------------------------------- |
2789| [Decimal](#decimal) | 返回符合范围的Decimal对象实例。 |
2790
2791**错误码:**
2792
2793以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
2794
2795| 错误码ID | 错误信息                                                     |
2796| -------- | ------------------------------------------------------------ |
2797| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2798| 10200001 | The value of 'min' is out of range.                          |
2799
2800**示例:**
2801
2802```ts
2803let a: Decimal = Decimal.clamp(10.1, 0, 10);
2804console.info("test Decimal clamp:" + a.toString()); // '10'
2805```
2806
2807### add
2808
2809static add(x: Value, y: Value): Decimal
2810
2811返回一个值为x加y的和的Decimal对象。
2812
2813使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2814
2815**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2816
2817**系统能力**:SystemCapability.Utils.Lang
2818
2819**参数:**
2820
2821| 参数名 | 类型            | 必填 | 说明               |
2822| ------ | --------------- | ---- | ------------------ |
2823| x      | [Value](#value) | 是   | 加法的一个加数。   |
2824| y      | [Value](#value) | 是   | 加法的另一个加数。 |
2825
2826**返回值:**
2827
2828| 类型                | 说明                              |
2829| ------------------- | --------------------------------- |
2830| [Decimal](#decimal) | 返回加法运算后的Decimal对象实例。 |
2831
2832**错误码:**
2833
2834以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2835
2836| 错误码ID | 错误信息                                                     |
2837| -------- | ------------------------------------------------------------ |
2838| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2839
2840**示例:**
2841
2842```ts
2843let a: Decimal = Decimal.add(0.5, 0.5);
2844console.info("test Decimal add:" + a.toString()); // '1'
2845```
2846
2847### sum
2848
2849static sum(...n: Value[]): Decimal
2850
2851返回一个值为数组的和的Decimal对象。
2852
2853使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2854
2855**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2856
2857**系统能力**:SystemCapability.Utils.Lang
2858
2859**参数:**
2860
2861| 参数名 | 类型              | 必填 | 说明         |
2862| ------ | ----------------- | ---- | ------------ |
2863| n      | [Value](#value)[] | 是   | 加数的数组。 |
2864
2865**返回值:**
2866
2867| 类型                | 说明                              |
2868| ------------------- | --------------------------------- |
2869| [Decimal](#decimal) | 返回加法运算后的Decimal对象实例。 |
2870
2871**错误码:**
2872
2873以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2874
2875| 错误码ID | 错误信息                                                     |
2876| -------- | ------------------------------------------------------------ |
2877| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2878
2879**示例:**
2880
2881```ts
2882let a: Decimal = Decimal.sum(0.5, 0.5);
2883console.info("test Decimal sum:" + a.toString()); // '1'
2884```
2885
2886### sub
2887
2888static sub(x: Value, y: Value): Decimal
2889
2890返回一个值为x减y的差的Decimal对象。
2891
2892使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2893
2894**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2895
2896**系统能力**:SystemCapability.Utils.Lang
2897
2898**参数:**
2899
2900| 参数名 | 类型            | 必填 | 说明           |
2901| ------ | --------------- | ---- | -------------- |
2902| x      | [Value](#value) | 是   | 减法的被减数。 |
2903| y      | [Value](#value) | 是   | 减法的减数。   |
2904
2905**返回值:**
2906
2907| 类型                | 说明                              |
2908| ------------------- | --------------------------------- |
2909| [Decimal](#decimal) | 返回减法运算后的Decimal对象实例。 |
2910
2911**错误码:**
2912
2913以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2914
2915| 错误码ID | 错误信息                                                     |
2916| -------- | ------------------------------------------------------------ |
2917| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2918
2919**示例:**
2920
2921```ts
2922let a: Decimal = Decimal.sub(1, 0.5);
2923console.info("test Decimal sub:" + a.toString()); // '0.5'
2924```
2925
2926### mul
2927
2928static mul(x: Value, y: Value): Decimal
2929
2930返回一个值为x乘以y的积的Decimal对象。
2931
2932使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2933
2934**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2935
2936**系统能力**:SystemCapability.Utils.Lang
2937
2938**参数:**
2939
2940| 参数名 | 类型            | 必填 | 说明           |
2941| ------ | --------------- | ---- | -------------- |
2942| x      | [Value](#value) | 是   | 乘法的被乘数。 |
2943| y      | [Value](#value) | 是   | 乘法的乘数。   |
2944
2945**返回值:**
2946
2947| 类型                | 说明                              |
2948| ------------------- | --------------------------------- |
2949| [Decimal](#decimal) | 返回乘法运算后的Decimal对象实例。 |
2950
2951**错误码:**
2952
2953以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2954
2955| 错误码ID | 错误信息                                                     |
2956| -------- | ------------------------------------------------------------ |
2957| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2958
2959**示例:**
2960
2961```ts
2962let a: Decimal = Decimal.mul(1, 0.5);
2963console.info("test Decimal mul:" + a.toString()); // '0.5'
2964```
2965
2966### div
2967
2968static div(x: Value, y: Value): Decimal
2969
2970返回一个值为x除以y的商的Decimal对象。
2971
2972使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
2973
2974**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
2975
2976**系统能力**:SystemCapability.Utils.Lang
2977
2978**参数:**
2979
2980| 参数名 | 类型            | 必填 | 说明           |
2981| ------ | --------------- | ---- | -------------- |
2982| x      | [Value](#value) | 是   | 除法的被除数。 |
2983| y      | [Value](#value) | 是   | 除法的除数。   |
2984
2985**返回值:**
2986
2987| 类型                | 说明                              |
2988| ------------------- | --------------------------------- |
2989| [Decimal](#decimal) | 返回除法运算后的Decimal对象实例。 |
2990
2991**错误码:**
2992
2993以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
2994
2995| 错误码ID | 错误信息                                                     |
2996| -------- | ------------------------------------------------------------ |
2997| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
2998
2999
3000**示例:**
3001
3002```ts
3003let a: Decimal = Decimal.div(1, 0.5);
3004console.info("test Decimal div:" + a.toString()); // '2'
3005```
3006
3007### mod
3008
3009static mod(x: Value, y: Value): Decimal
3010
3011返回一个新的Decimal对象,其值是x除以y的模。
3012
3013使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3014
3015**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3016
3017**系统能力**:SystemCapability.Utils.Lang
3018
3019**参数:**
3020
3021| 参数名 | 类型            | 必填 | 说明               |
3022| ------ | --------------- | ---- | ------------------ |
3023| x      | [Value](#value) | 是   | 模除运算的被除数。 |
3024| y      | [Value](#value) | 是   | 模除运算的除数。   |
3025
3026**返回值:**
3027
3028| 类型                | 说明                              |
3029| ------------------- | --------------------------------- |
3030| [Decimal](#decimal) | 返回模除运算后的Decimal对象实例。 |
3031
3032**错误码:**
3033
3034以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3035
3036| 错误码ID | 错误信息                                                     |
3037| -------- | ------------------------------------------------------------ |
3038| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3039
3040**示例:**
3041
3042```ts
3043let a: Decimal = Decimal.mod(2, 1);
3044console.info("test Decimal mod:" + a.toString()); // '0'
3045```
3046
3047### sqrt
3048
3049static sqrt(n: Value): Decimal
3050
3051返回一个值为n的平方根的Decimal对象。
3052
3053使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3054
3055**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3056
3057**系统能力**:SystemCapability.Utils.Lang
3058
3059**参数:**
3060
3061| 参数名 | 类型            | 必填 | 说明           |
3062| ------ | --------------- | ---- | -------------- |
3063| n      | [Value](#value) | 是   | 取平方根的值。 |
3064
3065**返回值:**
3066
3067| 类型                | 说明                                |
3068| ------------------- | ----------------------------------- |
3069| [Decimal](#decimal) | 返回平方根运算后的Decimal对象实例。 |
3070
3071**错误码:**
3072
3073以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3074
3075| 错误码ID | 错误信息                                                     |
3076| -------- | ------------------------------------------------------------ |
3077| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3078
3079**示例:**
3080
3081```ts
3082let a: Decimal = Decimal.sqrt(3);
3083console.info("test Decimal sqrt:" + a.toString()); // '1.7320508075688772935'
3084```
3085
3086### cbrt
3087
3088static cbrt(n: Value): Decimal
3089
3090返回一个值为n的立方根的Decimal对象。
3091
3092使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3093
3094**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3095
3096**系统能力**:SystemCapability.Utils.Lang
3097
3098**参数:**
3099
3100| 参数名 | 类型            | 必填 | 说明           |
3101| ------ | --------------- | ---- | -------------- |
3102| n      | [Value](#value) | 是   | 取立方根的值。 |
3103
3104**返回值:**
3105
3106| 类型                | 说明                                |
3107| ------------------- | ----------------------------------- |
3108| [Decimal](#decimal) | 返回立方根运算后的Decimal对象实例。 |
3109
3110**错误码:**
3111
3112以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3113
3114| 错误码ID | 错误信息                                                     |
3115| -------- | ------------------------------------------------------------ |
3116| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3117
3118**示例:**
3119
3120```ts
3121let a: Decimal = Decimal.cbrt(3);
3122console.info("test Decimal cbrt:" + a.toString()); // '1.4422495703074083823'
3123```
3124
3125### pow
3126
3127static pow(base: Value, exponent: Value): Decimal
3128
3129返回一个值为base的exponent次幂的Decimal对象,按照[DecimalConfig.precision](#decimalconfig)设置有效位数,按照[DecimalConfig.rounding](#decimalconfig)设置舍入模式。
3130
3131**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3132
3133**系统能力**:SystemCapability.Utils.Lang
3134
3135**参数:**
3136
3137| 参数名   | 类型            | 必填 | 说明               |
3138| -------- | --------------- | ---- | ------------------ |
3139| base     | [Value](#value) | 是   | 幂运算的底数的值。 |
3140| exponent | [Value](#value) | 是   | 幂运算的幂的值。   |
3141
3142**返回值:**
3143
3144| 类型                | 说明                            |
3145| ------------------- | ------------------------------- |
3146| [Decimal](#decimal) | 返回幂运算后的Decimal对象实例。 |
3147
3148**错误码:**
3149
3150以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3151
3152| 错误码ID | 错误信息                                                     |
3153| -------- | ------------------------------------------------------------ |
3154| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3155| 10200060 | Precision limit exceeded.                                    |
3156
3157**示例:**
3158
3159```ts
3160let a: Decimal = Decimal.pow(3, -2);
3161console.info("test Decimal pow:" + a.toString()); // '0.11111111111111111111'
3162```
3163
3164### exp
3165
3166static exp(n: Value): Decimal
3167
3168返回一个值为n的自然指数的Decimal对象。
3169
3170使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3171
3172**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3173
3174**系统能力**:SystemCapability.Utils.Lang
3175
3176**参数:**
3177
3178| 参数名 | 类型            | 必填 | 说明                 |
3179| ------ | --------------- | ---- | -------------------- |
3180| n      | [Value](#value) | 是   | 需要求自然指数的值。 |
3181
3182**返回值:**
3183
3184| 类型                | 说明                                  |
3185| ------------------- | ------------------------------------- |
3186| [Decimal](#decimal) | 返回自然指数运算后的Decimal对象实例。 |
3187
3188**错误码:**
3189
3190以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3191
3192| 错误码ID | 错误信息                                                     |
3193| -------- | ------------------------------------------------------------ |
3194| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3195| 10200060 | Precision limit exceeded.                                    |
3196
3197**示例:**
3198
3199```ts
3200let a: Decimal = Decimal.exp(2);
3201console.info("test Decimal exp:" + a.toString()); // '7.3890560989306502272'
3202```
3203
3204### log
3205
3206static log(n: Value, base: Value): Decimal
3207
3208返回一个值为以base为底n的对数的Decimal对象。
3209
3210使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3211
3212**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3213
3214**系统能力**:SystemCapability.Utils.Lang
3215
3216**参数:**
3217
3218| 参数名 | 类型            | 必填 | 说明             |
3219| ------ | --------------- | ---- | ---------------- |
3220| n      | [Value](#value) | 是   | 对数运算的真数。 |
3221| base   | [Value](#value) | 是   | 对数运算的底。   |
3222
3223**返回值:**
3224
3225| 类型                | 说明                              |
3226| ------------------- | --------------------------------- |
3227| [Decimal](#decimal) | 返回对数运算后的Decimal对象实例。 |
3228
3229**错误码:**
3230
3231以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3232
3233| 错误码ID | 错误信息                                                     |
3234| -------- | ------------------------------------------------------------ |
3235| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3236| 10200060 | Precision limit exceeded.                                    |
3237
3238**示例:**
3239
3240```ts
3241let a: Decimal = Decimal.log(2, 256);
3242console.info("test Decimal log:" + a.toString()); // '0.125'
3243```
3244
3245### ln
3246
3247static ln(n: Value): Decimal
3248
3249返回一个值为n的自然对数的Decimal对象。
3250
3251使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3252
3253**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3254
3255**系统能力**:SystemCapability.Utils.Lang
3256
3257**参数:**
3258
3259| 参数名 | 类型            | 必填 | 说明             |
3260| ------ | --------------- | ---- | ---------------- |
3261| n      | [Value](#value) | 是   | 对数运算的真数。 |
3262
3263**返回值:**
3264
3265| 类型                | 说明                                  |
3266| ------------------- | ------------------------------------- |
3267| [Decimal](#decimal) | 返回自然对数运算后的Decimal对象实例。 |
3268
3269**错误码:**
3270
3271以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3272
3273| 错误码ID | 错误信息                                                     |
3274| -------- | ------------------------------------------------------------ |
3275| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3276| 10200060 | Precision limit exceeded.                                    |
3277
3278**示例:**
3279
3280```ts
3281let a: Decimal = Decimal.ln(1.23e+30);
3282console.info("test Decimal ln:" + a.toString()); // '69.284566959205696648'
3283```
3284
3285### log2
3286
3287static log2(n: Value): Decimal
3288
3289返回一个值为以2为底n的对数的Decimal对象。
3290
3291使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3292
3293**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3294
3295**系统能力**:SystemCapability.Utils.Lang
3296
3297**参数:**
3298
3299| 参数名 | 类型            | 必填 | 说明             |
3300| ------ | --------------- | ---- | ---------------- |
3301| n      | [Value](#value) | 是   | 对数运算的真数。 |
3302
3303**返回值:**
3304
3305| 类型                | 说明                                       |
3306| ------------------- | ------------------------------------------ |
3307| [Decimal](#decimal) | 返回以2为底的对数运算后的Decimal对象实例。 |
3308
3309**错误码:**
3310
3311以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3312
3313| 错误码ID | 错误信息                                                     |
3314| -------- | ------------------------------------------------------------ |
3315| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3316| 10200060 | Precision limit exceeded.                                    |
3317
3318**示例:**
3319
3320```ts
3321let a: Decimal = Decimal.log2(4);
3322console.info("test Decimal log2:" + a.toString()); // '2'
3323```
3324
3325### log10
3326
3327static log10(n: Value): Decimal
3328
3329返回一个值为以10为底n的对数的Decimal对象。
3330
3331使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3332
3333**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3334
3335**系统能力**:SystemCapability.Utils.Lang
3336
3337**参数:**
3338
3339| 参数名 | 类型            | 必填 | 说明             |
3340| ------ | --------------- | ---- | ---------------- |
3341| n      | [Value](#value) | 是   | 对数运算的真数。 |
3342
3343**返回值:**
3344
3345| 类型                | 说明                                        |
3346| ------------------- | ------------------------------------------- |
3347| [Decimal](#decimal) | 返回以10为底的对数运算后的Decimal对象实例。 |
3348
3349**错误码:**
3350
3351以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3352
3353| 错误码ID | 错误信息                                                     |
3354| -------- | ------------------------------------------------------------ |
3355| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3356| 10200060 | Precision limit exceeded.                                    |
3357
3358**示例:**
3359
3360```ts
3361let a: Decimal = Decimal.log10(10000);
3362console.info("test Decimal log10:" + a.toString()); // '4'
3363```
3364
3365### cos
3366
3367static cos(n: Value): Decimal
3368
3369返回一个新的Decimal,其值是n的余弦值。
3370
3371使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3372
3373**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3374
3375**系统能力**:SystemCapability.Utils.Lang
3376
3377**参数:**
3378
3379| 参数名 | 类型            | 必填 | 说明             |
3380| ------ | --------------- | ---- | ---------------- |
3381| n      | [Value](#value) | 是   | 要求余弦值的值。 |
3382
3383**返回值:**
3384
3385| 类型                | 说明                                   |
3386| ------------------- | -------------------------------------- |
3387| [Decimal](#decimal) | 返回n的余弦值对应的Decimal对象实例。 |
3388
3389**错误码:**
3390
3391以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3392
3393| 错误码ID | 错误信息                                                     |
3394| -------- | ------------------------------------------------------------ |
3395| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3396
3397**示例:**
3398
3399```ts
3400let a: Decimal = Decimal.cos(-0.25);
3401console.info("test Decimal cos:" + a.toString()); // '0.96891242171064478414'
3402```
3403
3404### sin
3405
3406static sin(n: Value): Decimal
3407
3408返回一个新的Decimal,其值是n的正弦值。
3409
3410使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3411
3412**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3413
3414**系统能力**:SystemCapability.Utils.Lang
3415
3416**参数:**
3417
3418| 参数名 | 类型            | 必填 | 说明             |
3419| ------ | --------------- | ---- | ---------------- |
3420| n      | [Value](#value) | 是   | 要求正弦值的值。 |
3421
3422**返回值:**
3423
3424| 类型                | 说明                                   |
3425| ------------------- | -------------------------------------- |
3426| [Decimal](#decimal) | 返回n的正弦值对应的Decimal对象实例。 |
3427
3428**错误码:**
3429
3430以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3431
3432| 错误码ID | 错误信息                                                     |
3433| -------- | ------------------------------------------------------------ |
3434| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3435
3436**示例:**
3437
3438```ts
3439let a: Decimal = Decimal.sin(0.75);
3440console.info("test Decimal sin:" + a.toString()); // '0.68163876002333416673'
3441```
3442
3443### tan
3444
3445static tan(n: Value): Decimal
3446
3447返回一个新的Decimal,其值是n的正切值。
3448
3449使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3450
3451**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3452
3453**系统能力**:SystemCapability.Utils.Lang
3454
3455**参数:**
3456
3457| 参数名 | 类型            | 必填 | 说明             |
3458| ------ | --------------- | ---- | ---------------- |
3459| n      | [Value](#value) | 是   | 要求正切值的值。 |
3460
3461**返回值:**
3462
3463| 类型                | 说明                                   |
3464| ------------------- | -------------------------------------- |
3465| [Decimal](#decimal) | 返回n的正切值对应的Decimal对象实例。 |
3466
3467**错误码:**
3468
3469以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3470
3471| 错误码ID | 错误信息                                                     |
3472| -------- | ------------------------------------------------------------ |
3473| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3474
3475**示例:**
3476
3477```ts
3478let a: Decimal = Decimal.tan(0.75);
3479console.info("test Decimal tan:" + a.toString()); // '0.93159645994407246117'
3480```
3481
3482### cosh
3483
3484static cosh(n: Value): Decimal
3485
3486返回一个新的Decimal,其值是n的双曲余弦值。
3487
3488使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3489
3490**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3491
3492**系统能力**:SystemCapability.Utils.Lang
3493
3494**参数:**
3495
3496| 参数名 | 类型            | 必填 | 说明                   |
3497| ------ | --------------- | ---- | ---------------------- |
3498| n      | [Value](#value) | 是   | 需要求双曲余弦值的值。 |
3499
3500**返回值:**
3501
3502| 类型                | 说明                                       |
3503| ------------------- | ------------------------------------------ |
3504| [Decimal](#decimal) | 返回n的双曲余弦值对应的Decimal对象实例。 |
3505
3506**错误码:**
3507
3508以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3509
3510| 错误码ID | 错误信息                                                     |
3511| -------- | ------------------------------------------------------------ |
3512| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3513
3514**示例:**
3515
3516```ts
3517let a: Decimal = Decimal.cosh(0.5);
3518console.info("test Decimal cosh:" + a.toString()); // '1.1276259652063807852'
3519```
3520
3521### sinh
3522
3523static sinh(n: Value): Decimal
3524
3525返回一个新的Decimal,其值是n的双曲正弦值。
3526
3527使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3528
3529**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3530
3531**系统能力**:SystemCapability.Utils.Lang
3532
3533**参数:**
3534
3535| 参数名 | 类型            | 必填 | 说明                   |
3536| ------ | --------------- | ---- | ---------------------- |
3537| n      | [Value](#value) | 是   | 需要求双曲正弦值的值。 |
3538
3539**返回值:**
3540
3541| 类型                | 说明                                       |
3542| ------------------- | ------------------------------------------ |
3543| [Decimal](#decimal) | 返回n的双曲正弦值对应的Decimal对象实例。 |
3544
3545**错误码:**
3546
3547以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3548
3549| 错误码ID | 错误信息                                                     |
3550| -------- | ------------------------------------------------------------ |
3551| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3552
3553**示例:**
3554
3555```ts
3556let a: Decimal = Decimal.sinh(0.5);
3557console.info("test Decimal sinh:" + a.toString()); // '0.52109530549374736162'
3558```
3559
3560### tanh
3561
3562static tanh(n: Value): Decimal
3563
3564返回一个新的Decimal,其值是n的双曲正切值。
3565
3566使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3567
3568**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3569
3570**系统能力**:SystemCapability.Utils.Lang
3571
3572**参数:**
3573
3574| 参数名 | 类型            | 必填 | 说明                   |
3575| ------ | --------------- | ---- | ---------------------- |
3576| n      | [Value](#value) | 是   | 需要求双曲正切值的值。 |
3577
3578**返回值:**
3579
3580| 类型                | 说明                                       |
3581| ------------------- | ------------------------------------------ |
3582| [Decimal](#decimal) | 返回n的双曲正切值对应的Decimal对象实例。 |
3583
3584**错误码:**
3585
3586以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3587
3588| 错误码ID | 错误信息                                                     |
3589| -------- | ------------------------------------------------------------ |
3590| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3591
3592**示例:**
3593
3594```ts
3595let a: Decimal = Decimal.tanh(0.5);
3596console.info("test Decimal tanh:" + a.toString()); // '0.4621171572600097585'
3597```
3598
3599### acos
3600
3601static acos(n: Value): Decimal
3602
3603返回一个新的Decimal,其值是n的反余弦值。
3604
3605使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3606
3607**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3608
3609**系统能力**:SystemCapability.Utils.Lang
3610
3611**参数:**
3612
3613| 参数名 | 类型            | 必填 | 说明                 |
3614| ------ | --------------- | ---- | -------------------- |
3615| n      | [Value](#value) | 是   | 需要求反余弦值的值。 |
3616
3617**返回值:**
3618
3619| 类型                | 说明                                   |
3620| ------------------- | -------------------------------------- |
3621| [Decimal](#decimal) | 返回n的反余弦值对应的Decimal对象实例。 |
3622
3623**错误码**:
3624
3625以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3626
3627| 错误码ID | 错误信息                                                     |
3628| -------- | ------------------------------------------------------------ |
3629| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3630| 10200060 | Precision limit exceeded.                                    |
3631
3632**示例:**
3633
3634```ts
3635let a: Decimal = Decimal.acos(0.5);
3636console.info("test Decimal acos:" + a.toString()); // '1.0471975511965977462'
3637```
3638
3639### asin
3640
3641static asin(n: Value): Decimal
3642
3643返回一个新的Decimal,其值是n的反正弦值。
3644
3645使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3646
3647**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3648
3649**系统能力**:SystemCapability.Utils.Lang
3650
3651**参数:**
3652
3653| 参数名 | 类型            | 必填 | 说明                 |
3654| ------ | --------------- | ---- | -------------------- |
3655| n      | [Value](#value) | 是   | 需要求反正弦值的值。 |
3656
3657**返回值:**
3658
3659| 类型                | 说明                                   |
3660| ------------------- | -------------------------------------- |
3661| [Decimal](#decimal) | 返回n的反正弦值对应的Decimal对象实例。 |
3662
3663**错误码**:
3664
3665以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3666
3667| 错误码ID | 错误信息                                                     |
3668| -------- | ------------------------------------------------------------ |
3669| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3670| 10200060 | Precision limit exceeded.                                    |
3671
3672**示例:**
3673
3674```ts
3675let a: Decimal = Decimal.asin(0.75);
3676console.info("test Decimal asin:" + a.toString()); // '0.84806207898148100805'
3677```
3678
3679### atan
3680
3681static atan(n: Value): Decimal
3682
3683返回一个新的Decimal,其值是n的反正切值。
3684
3685使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3686
3687**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3688
3689**系统能力**:SystemCapability.Utils.Lang
3690
3691**参数:**
3692
3693| 参数名 | 类型            | 必填 | 说明                 |
3694| ------ | --------------- | ---- | -------------------- |
3695| n      | [Value](#value) | 是   | 需要求反正切值的值。 |
3696
3697**返回值:**
3698
3699| 类型                | 说明                                   |
3700| ------------------- | -------------------------------------- |
3701| [Decimal](#decimal) | 返回n的反正切值对应的Decimal对象实例。 |
3702
3703**错误码**:
3704
3705以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3706
3707| 错误码ID | 错误信息                                                     |
3708| -------- | ------------------------------------------------------------ |
3709| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3710| 10200060 | Precision limit exceeded.                                    |
3711
3712**示例:**
3713
3714```ts
3715let a: Decimal = Decimal.atan(0.75);
3716console.info("test Decimal atan:" + a.toString()); // '0.6435011087932843868'
3717```
3718
3719### acosh
3720
3721static acosh(n: Value): Decimal
3722
3723返回一个新的Decimal,其值是n的双曲余弦值的倒数。
3724
3725使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3726
3727**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3728
3729**系统能力**:SystemCapability.Utils.Lang
3730
3731**参数:**
3732
3733| 参数名 | 类型            | 必填 | 说明                       |
3734| ------ | --------------- | ---- | -------------------------- |
3735| n      | [Value](#value) | 是   | 需要求双曲余弦的倒数的值。 |
3736
3737**返回值:**
3738
3739| 类型                | 说明                                           |
3740| ------------------- | ---------------------------------------------- |
3741| [Decimal](#decimal) | 返回n的双曲余弦的倒数对应的Decimal对象实例。 |
3742
3743**错误码**:
3744
3745以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3746
3747| 错误码ID | 错误信息                                                     |
3748| -------- | ------------------------------------------------------------ |
3749| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3750| 10200060 | Precision limit exceeded.                                    |
3751
3752**示例:**
3753
3754```ts
3755let a: Decimal = Decimal.acosh(50);
3756console.info("test Decimal acosh:" + a.toString()); // '4.6050701709847571595'
3757```
3758
3759### asinh
3760
3761static asinh(n: Value): Decimal
3762
3763返回一个新的Decimal,其值是n的双曲正弦值的倒数。
3764
3765使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3766
3767**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3768
3769**系统能力**:SystemCapability.Utils.Lang
3770
3771**参数:**
3772
3773| 参数名 | 类型            | 必填 | 说明                       |
3774| ------ | --------------- | ---- | -------------------------- |
3775| n      | [Value](#value) | 是   | 需要求双曲正弦的倒数的值。 |
3776
3777**返回值:**
3778
3779| 类型                | 说明                                           |
3780| ------------------- | ---------------------------------------------- |
3781| [Decimal](#decimal) | 返回n的双曲正弦的倒数对应的Decimal对象实例。 |
3782
3783**错误码**:
3784
3785以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3786
3787| 错误码ID | 错误信息                                                     |
3788| -------- | ------------------------------------------------------------ |
3789| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3790| 10200060 | Precision limit exceeded.                                    |
3791
3792**示例:**
3793
3794```ts
3795let a: Decimal = Decimal.asinh(50);
3796console.info("test Decimal asinh:" + a.toString()); // '4.6052701709914238266'
3797```
3798
3799### atanh
3800
3801static atanh(n: Value): Decimal
3802
3803返回一个新的Decimal,其值是n的双曲正切值的倒数。
3804
3805使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3806
3807**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3808
3809**系统能力**:SystemCapability.Utils.Lang
3810
3811**参数:**
3812
3813| 参数名 | 类型            | 必填 | 说明                       |
3814| ------ | --------------- | ---- | -------------------------- |
3815| n      | [Value](#value) | 是   | 需要求双曲正切的倒数的值。 |
3816
3817**返回值:**
3818
3819| 类型                | 说明                                           |
3820| ------------------- | ---------------------------------------------- |
3821| [Decimal](#decimal) | 返回n的双曲正切的倒数对应的Decimal对象实例。 |
3822
3823**错误码**:
3824
3825以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3826
3827| 错误码ID | 错误信息                                                     |
3828| -------- | ------------------------------------------------------------ |
3829| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3830| 10200060 | Precision limit exceeded.                                    |
3831
3832**示例:**
3833
3834```ts
3835let a: Decimal = Decimal.atanh(0.75);
3836console.info("test Decimal atanh:" + a.toString()); // '0.97295507452765665255'
3837```
3838
3839### atan2
3840
3841static atan2(y: Value, x: Value): Decimal
3842
3843返回一个新的Decimal,其值是为-π到π范围内的y/x反正切值。
3844
3845使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3846
3847**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3848
3849**系统能力**:SystemCapability.Utils.Lang
3850
3851**参数:**
3852
3853| 参数名 | 类型            | 必填 | 说明           |
3854| ------ | --------------- | ---- | -------------- |
3855| y      | [Value](#value) | 是   | 除法的被除数。 |
3856| x      | [Value](#value) | 是   | 除法的除数。   |
3857
3858**返回值:**
3859
3860| 类型                | 说明                                                       |
3861| ------------------- | ---------------------------------------------------------- |
3862| [Decimal](#decimal) | 返回-pi 到 pi 范围内的"y/x"反正切值对应的Decimal对象实例。 |
3863
3864**错误码**:
3865
3866以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
3867
3868| 错误码ID | 错误信息                                                     |
3869| -------- | ------------------------------------------------------------ |
3870| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3871| 10200060 | Precision limit exceeded.                                    |
3872
3873**示例:**
3874
3875```ts
3876let a: Decimal = Decimal.atan2(2, 3);
3877console.info("test Decimal atan2:" + a.toString()); // '0.58800260354756755125'
3878```
3879
3880### hypot
3881
3882static hypot(...n: Value[]): Decimal
3883
3884返回一个新的Decimal,其值为参数平方和的平方根。
3885
3886使用[DecimalConfig.precision](#decimalconfig)的值进行有效数字的保留,使用[DecimalConfig.rounding](#decimalconfig)的值设置舍入模式。
3887
3888**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3889
3890**系统能力**:SystemCapability.Utils.Lang
3891
3892**参数:**
3893
3894| 参数名 | 类型              | 必填 | 说明                 |
3895| ------ | ----------------- | ---- | -------------------- |
3896| n      | [Value](#value)[] | 是   | 需要求平方和的数组。 |
3897
3898**返回值:**
3899
3900| 类型                | 说明                                              |
3901| ------------------- | ------------------------------------------------- |
3902| [Decimal](#decimal) | 返回值为所有参数平方和的平方根的Decimal对象实例。 |
3903
3904**错误码**:
3905
3906以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3907
3908| 错误码ID | 错误信息                                                     |
3909| -------- | ------------------------------------------------------------ |
3910| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3911
3912**示例:**
3913
3914```ts
3915let a: Decimal = Decimal.hypot(2, 3, 4);
3916console.info("test Decimal hypot:" + a.toString()); // '5.3851648071345040313'
3917```
3918
3919### max
3920
3921static max(...n: Value[]): Decimal
3922
3923返回一个值为所有参数中最大值的Decimal对象。
3924
3925**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3926
3927**系统能力**:SystemCapability.Utils.Lang
3928
3929**参数:**
3930
3931| 参数名 | 类型              | 必填 | 说明                 |
3932| ------ | ----------------- | ---- | -------------------- |
3933| n      | [Value](#value)[] | 是   | 需要求最大值的数组。 |
3934
3935**返回值:**
3936
3937| 类型                | 说明                                      |
3938| ------------------- | ----------------------------------------- |
3939| [Decimal](#decimal) | 返回所有参数中的最大值的Decimal对象实例。 |
3940
3941**错误码**:
3942
3943以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3944
3945| 错误码ID | 错误信息                                                     |
3946| -------- | ------------------------------------------------------------ |
3947| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3948
3949**示例:**
3950
3951```ts
3952let a: Decimal = Decimal.max(2, 3, 4);
3953console.info("test Decimal max:" + a.toString()); // '4'
3954```
3955
3956### min
3957
3958static min(...n: Value[]): Decimal
3959
3960返回一个值为所有参数中最小值的Decimal对象。
3961
3962**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
3963
3964**系统能力**:SystemCapability.Utils.Lang
3965
3966**参数:**
3967
3968| 参数名 | 类型            | 必填 | 说明                 |
3969| ------ | --------------- | ---- | -------------------- |
3970| n      | [Value](#value) | 是   | 需要求最小值的数组。 |
3971
3972**返回值:**
3973
3974| 类型                | 说明                                      |
3975| ------------------- | ----------------------------------------- |
3976| [Decimal](#decimal) | 返回所有参数中的最小值的Decimal对象实例。 |
3977
3978**错误码**:
3979
3980以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
3981
3982| 错误码ID | 错误信息                                                     |
3983| -------- | ------------------------------------------------------------ |
3984| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
3985
3986**示例:**
3987
3988```ts
3989let a: Decimal = Decimal.min(2, 3, 4);
3990console.info("test Decimal min:" + a.toString()); // '2'
3991```
3992
3993### random
3994
3995static random(): Decimal
3996
3997返回一个值为大于等于0小于1的随机值的Decimal对象。
3998
3999**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
4000
4001**系统能力**:SystemCapability.Utils.Lang
4002
4003**返回值:**
4004
4005| 类型                | 说明                                      |
4006| ------------------- | ----------------------------------------- |
4007| [Decimal](#decimal) | 大于等于0小于1的随机值的Decimal对象实例。 |
4008
4009**错误码:**
4010
4011以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
4012
4013| 错误码ID | 错误信息            |
4014| -------- | ------------------- |
4015| 10200061 | Crypto unavailable. |
4016
4017**示例:**
4018
4019```ts
4020let a: Decimal = Decimal.random();
4021```
4022
4023### random
4024
4025static random(significantDigits: number): Decimal
4026
4027返回一个值为大于等于0小于1的随机值的Decimal对象,随机值保留significantDigits位有效数字。
4028
4029**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
4030
4031**系统能力**:SystemCapability.Utils.Lang
4032
4033**参数:**
4034
4035| 参数名            | 类型   | 必填 | 说明                   |
4036| ----------------- | ------ | ---- | ---------------------- |
4037| significantDigits | number | 是   | 随机值保留的有效数字。 |
4038
4039**返回值:**
4040
4041| 类型                | 说明                                      |
4042| ------------------- | ----------------------------------------- |
4043| [Decimal](#decimal) | 大于等于0小于1的随机值的Decimal对象实例。 |
4044
4045**错误码:**
4046
4047以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
4048
4049| 错误码ID | 错误信息                                                     |
4050| -------- | ------------------------------------------------------------ |
4051| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
4052| 10200061 | Crypto unavailable.                                          |
4053
4054**示例:**
4055
4056```ts
4057let a: Decimal = Decimal.random(20);
4058```
4059
4060### sign
4061
4062static sign(n: Value): number
4063
4064根据参数的值进行判断返回对应的值:当n>0返回1,当n<0返回-1,当n==0返回0,当n==-0返回-0,否则返回NaN。
4065
4066**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
4067
4068**系统能力**:SystemCapability.Utils.Lang
4069
4070**参数:**
4071
4072| 参数名 | 类型            | 必填 | 说明           |
4073| ------ | --------------- | ---- | -------------- |
4074| n      | [Value](#value) | 是   | 需要判断的值。 |
4075
4076**返回值:**
4077
4078| 类型   | 说明                               |
4079| ------ | ---------------------------------- |
4080| number | 根据参数的值进行判断返回对应的值。 |
4081
4082**错误码:**
4083
4084以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
4085
4086| 错误码ID | 错误信息                                                     |
4087| -------- | ------------------------------------------------------------ |
4088| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
4089
4090**示例:**
4091
4092```ts
4093let a: number = Decimal.sign(2);
4094console.info("test Decimal sign:" + a); // '1'
4095```
4096
4097### round
4098
4099static round(n: Value): Decimal
4100
4101返回一个新的Decimal,其值是使用[DecimalConfig.rounding](#decimalconfig)模式舍入为整数的n。
4102
4103**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
4104
4105**系统能力**:SystemCapability.Utils.Lang
4106
4107**参数:**
4108
4109| 参数名 | 类型            | 必填 | 说明           |
4110| ------ | --------------- | ---- | -------------- |
4111| n      | [Value](#value) | 是   | 需要舍入的值。 |
4112
4113**返回值:**
4114
4115| 类型                | 说明                                      |
4116| ------------------- | ----------------------------------------- |
4117| [Decimal](#decimal) | 返回舍入之后的整数对应的Decimal对象实例。 |
4118
4119**错误码:**
4120
4121以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
4122
4123| 错误码ID | 错误信息                                                     |
4124| -------- | ------------------------------------------------------------ |
4125| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
4126
4127**示例:**
4128
4129```ts
4130let x = 3.3333333333333;
4131let a = Decimal.round(x);
4132console.info("test Decimal round:" + a.toString()); // '3'
4133```
4134
4135### set
4136
4137static set(object: DecimalConfig):void
4138
4139用于设置Decimal的配置属性。
4140
4141**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
4142
4143**系统能力**:SystemCapability.Utils.Lang
4144
4145**参数:**
4146
4147| 参数名 | 类型                            | 必填 | 说明                 |
4148| ------ | ------------------------------- | ---- | -------------------- |
4149| object | [DecimalConfig](#decimalconfig) | 是   | 需要配置的属性集合。 |
4150
4151**错误码:**
4152
4153以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
4154
4155| 错误码ID | 错误信息                                                     |
4156| -------- | ------------------------------------------------------------ |
4157| 401      | Parameter error. Possible causes:<br/>1. Incorrect parameter types;<br/>2. Parameter verification failed. |
4158| 10200001 | The value of 'DecimalConfig.properties' is out of range.     |
4159| 10200061 | Crypto unavailable.                                          |
4160
4161**示例:**
4162
4163```ts
4164let a : Decimal = new Decimal(1.2345678901234567);
4165Decimal.set({
4166    precision: 5,
4167    rounding: 4,
4168    toExpNeg: -7,
4169    toExpPos: 7,
4170    maxE: 9e15,
4171    minE: -9e15,
4172    modulo: 1,
4173    crypto: false
4174})
4175let b : Decimal = a.add(0.5);
4176console.info("test Decimal set:" + b.toString()); // "1.7346"
4177// 将配置属性全部设置为默认值
4178Decimal.set({ defaults: true })
4179let c : Decimal = a.add(0.5);
4180console.info("test Decimal set:" + c.toString()); // "1.7345678901234567"
4181// 最大有效位数设置为10,其余配置属性设置为默认值
4182Decimal.set({ precision: 10, defaults: true })
4183let d : Decimal = a.add(0.5);
4184console.info("test Decimal set:" + d.toString()); // "1.73456789"
4185
4186// toExpNeg和toExpPos的用法
4187Decimal.set({ toExpNeg: -7 })
4188let x0 : Decimal = new Decimal(0.00000123) // '0.00000123'
4189let x1 : Decimal = new Decimal(0.000000123) // '1.23e-7'
4190
4191Decimal.set({ toExpPos: 2 })
4192let y0 : Decimal = new Decimal(12.3) // '12.3'
4193let y1 : Decimal = new Decimal(123) // '1.23e+2'
4194
4195// 所有数据均使用科学计数法表示
4196Decimal.set({ toExpPos: 0 })
4197
4198// minE和maxE的用法
4199Decimal.set({ minE: -500 })
4200let a0 : Decimal = new Decimal('1e-500') // '1e-500'
4201let a1 : Decimal = new Decimal('9.9e-501') // '0'
4202
4203Decimal.set({ minE: -3 })
4204let b0 : Decimal = new Decimal(0.001) // '0.001'
4205let b1 : Decimal = new Decimal(0.0001) // '0'
4206
4207Decimal.set({ maxE: 500 })
4208let c0 : Decimal = new Decimal('9.999e500') // '9.999e+500'
4209let c1 : Decimal = new Decimal('1e501') // 'Infinity'
4210
4211Decimal.set({ maxE: 4 })
4212let d0 : Decimal = new Decimal(99999) // '99999'
4213let d1 : Decimal = new Decimal(100000) // 'Infinity'
4214```