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