bignumber.js API

使用

  • 安裝 npm i bignumber.js
  • 引用
    • import BigNumber from 'bignumber.js'

文檔

加法 plus

  • 語法
    .plus(n [, base])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • base: number 進(jìn)制 (默認(rèn)為十進(jìn)制)
    • 返回值
      BigNumber
  • 舉例
0.1 + 0.2                       // 0.30000000000000004
x = new BigNumber(0.1)
y = x.plus(0.2)                 // '0.3'
BigNumber(0.7).plus(x).plus(y)  // '1'
x.plus('0.1', 8)                // '0.225'

減法 minus

  • 語法
    .minus(n [, base])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • base: number 進(jìn)制 (默認(rèn)為十進(jìn)制)
    • 返回值
      • BigNumber
  • 舉例
0.3 - 0.1                       // 0.19999999999999998
x = new BigNumber(0.3)
x.minus(0.1)                    // '0.2'
x.minus(0.6, 20)                // '0'

乘法 multipliedBy

  • 語法
    .times(n [, base])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • base: number 進(jìn)制 (默認(rèn)為十進(jìn)制)
    • 返回值
      • BigNumber
  • 舉例
0.6 * 3                         // 1.7999999999999998
x = new BigNumber(0.6)
y = x.multipliedBy(3)           // '1.8'
BigNumber('7e+500').times(y)    // '1.26e+501'
x.multipliedBy('-a', 16)        // '-6'

除法 dividedBy

  • 語法
    .div(n [, base])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • base: number 進(jìn)制 (默認(rèn)為十進(jìn)制)
    • 返回值
      • BigNumber
  • 舉例
x = new BigNumber(355)
y = new BigNumber(113)
x.dividedBy(y)                  // '3.14159292035398230088'
x.div(5)                        // '71'
x.div(47, 16)                   // '5'

除法-取整 dividedToIntegerBy

  • 語法
    .idiv(n [, base])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • base: number 進(jìn)制 (默認(rèn)為十進(jìn)制)
    • 返回值
      • BigNumber
  • 舉例
x = new BigNumber(5)
y = new BigNumber(3)
x.dividedToIntegerBy(y)         // '1'
x.idiv(0.7)                     // '7'
x.idiv('0.f', 16)               // '5'

除法-取余 modulo

  • 語法
    .mod(n [, base])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • base: number 進(jìn)制 (默認(rèn)為十進(jìn)制)
    • 返回值
      • BigNumber
  • 舉例
1 % 0.9                         // 0.09999999999999998
x = new BigNumber(1)
x.modulo(0.9)                   // '0.1'
y = new BigNumber(33)
y.mod('a', 33)                  // '3'

指數(shù)運(yùn)算 exponentiatedBy

  • 語法
    .pow(n [, m])
    • 參數(shù)值
      • n (必需): number|string|BigNumber 參與計(jì)算的數(shù)字
      • m: number|string|BigNumber 指數(shù)
    • 返回值
      • BigNumber
  • 舉例
Math.pow(0.7, 2)                // 0.48999999999999994
x = new BigNumber(0.7)
x.exponentiatedBy(2)            // '0.49'
BigNumber(3).pow(-2)            // '0.11111111111111111111'

開平方 squareRoot

  • 語法
    .sqrt()
  • 舉例
x = new BigNumber(16)
x.squareRoot()                  // '4'
y = new BigNumber(3)
y.sqrt()                        // '1.73205080756887729353'

四舍五入 toFixed

  • 語法
    .toFixed([dp [, rm]])
    • dp (必需): number 保留小數(shù)位數(shù)
    • rm: number
  • 舉例
x = 3.456
y = new BigNumber(x)
x.toFixed()                     // '3'
y.toFixed()                     // '3.456'
y.toFixed(0)                    // '3'
x.toFixed(2)                    // '3.46'
y.toFixed(2)                    // '3.46'
y.toFixed(2, 1)                 // '3.45'  (ROUND_DOWN)
x.toFixed(5)                    // '3.45600'
y.toFixed(5)                    // '3.45600'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容