sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

  • 方法-Function

  • 字符串-String

  • 数学-Math

    • 序言 👏
    • Decimal
      • 1.示例
    • sum【求和】🔥
    • avg 【平均】🔥
    • div【相除】🔥
    • mul【相乘】🔥
    • add【相加】🔥
    • div【相减】🔥
  • 文件-Buffer

  • 节点-dom

  • 拓展

  • nodejs

目录

Decimal

描述

返回 decimal.js-light 库,暴露出的 Decimal 对象。
具体使用文档请看 https://github.com/MikeMcl/decimal.js-light (opens new window)

# 1.示例

import { Decimal } from 'sf-utils2'

// 下面是从官网找出的示例

x = new Decimal(2)
y = new Decimal(3)

// x 乘以 y
x.mul(y).toFixed()

// x 除以 y
x.div(y).toFixed()

// x 相加 y
x.add(y).toFixed()

// x 相减 y
x.sub(y).toFixed()

// decimal.js-light
x.dividedBy(y).toString() // '0.66666666666666666666'
x.dividedBy(y).toDecimalPlaces(19).toString() // '0.6666666666666666667'

// Adjust the global configuration if required (these are the defaults)
Decimal.set({
  precision: 20,
  rounding: Decimal.ROUND_HALF_UP,
  toExpNeg: -7,
  toExpPos: 21
})

phi = new Decimal('1.61803398874989484820458683436563811772030917980576')

phi.toFixed(10) // '1.6180339887'

phi.times(2).minus(1).toPower(2).plus('1e-19').equals(5) // true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
上次更新: 2023/06/24, 19:35:48
序言 👏
sum【求和】🔥

← 序言 👏 sum【求和】🔥→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×