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