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