安装
Browser:
<script src='path/to/bignumber.js'></script>
$ npm install --save bignumber.js
var BigNumber = require('bignumber.js');
ES6 module (bignumber.mjs):
//import BigNumber from 'bignumber.js';
import {BigNumber} from 'bignumber.js';
AMD loader libraries such as requireJS:
require(['bignumber'], function(BigNumber) {
// Use BigNumber here in local scope. No global BigNumber.
});
使用
示例一:
bignumber.js 库中提供一个 BigNumber 的构造函数,参数必须是长度在15位以内的数字
x = new BigNumber(123.4567)
y = BigNumber('123456.7e-3')
z = new BigNumber(x)
x.isEqualTo(y) && y.isEqualTo(z) && x.isEqualTo(z) // true
示例二:
可以使用2-36进制的数,超过36进制的也可以,不过需要自定义字母来表示了。0-9 加上a-z刚好到36,所以超过36就无法表示了,需要你定义其他的符号来表示了。
x = new BigNumber(1011, 2) // "11"
y = new BigNumber('zz.9', 36) // "1295.25"
z = x.plus(y) // "1306.25"
示例三:
做小数运算
0.3 - 0.1 // 0.19999999999999998
x = new BigNumber(0.3)
x.minus(0.1) // "0.2"
x // "0.3"
示例四:
链式语法调用
x.dividedBy(y).plus(z).times(9)
x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').integerValue()
示例五:
有些方法名比较长的,有别名表示 比如 isEqualTo 和eq
x.squareRoot().dividedBy(y).exponentiatedBy(3).isEqualTo( x.sqrt().div(y).pow(3) ) // true
x.modulo(y).multipliedBy(z).eq( x.mod(y).times(z) ) // true
示例六:
和 js的Number类型类似,有一些常用的方法:
x = new BigNumber(255.5)
x.toExponential(5) // "2.55500e+2"
x.toFixed(5) // "255.50000"
x.toPrecision(5) // "255.50"
x.toNumber() // 255.5
toString方法
x.toString(16) // "ff.8"
There is also a toFormat method which may be useful for internationalisation
y = new BigNumber('1234567.898765')
y.toFormat(2) // "1,234,567.90"
The maximum number of decimal places of the result of an operation involving division (i.e. a division, square root, base conversion or negative power operation) is set using the config method of the BigNumber constructor.
The other arithmetic operations always give the exact result.
BigNumber.config({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 })
x = new BigNumber(2);
y = new BigNumber(3);
z = x.dividedBy(y) // "0.6666666667"
z.squareRoot() // "0.8164965809"
z.exponentiatedBy(-3) // "3.3749999995"
z.toString(2) // "0.1010101011"
z.multipliedBy(z) // "0.44444444448888888889"
z.multipliedBy(z).decimalPlaces(10) // "0.4444444445"
There is a toFraction method with an optional maximum denominator argument
y = new BigNumber(355)
pi = y.dividedBy(113) // "3.1415929204"
pi.toFraction() // [ "7853982301", "2500000000" ]
pi.toFraction(1000) // [ "355", "113" ]
and isNaN and isFinite methods, as NaN and Infinity are valid BigNumber values.
x = new BigNumber(NaN) // "NaN"
y = new BigNumber(Infinity) // "Infinity"
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
BigNumber的值以系数,指数和符号的形式,以十进制浮点格式进行存储
c 是coefficient 系数
c 是exponent 指数,最高位后有几个零
s 是sign 符号,也就是正负
x = new BigNumber(-123.456);
x.c // [ 123, 45600000000000 ] coefficient (i.e. significand)
x.e // 2 exponent
x.s // -1 sign
For advanced usage, multiple BigNumber constructors can be created, each with their own independent configuration which applies to all BigNumber's created from it.
// Set DECIMAL_PLACES for the original BigNumber constructor
BigNumber.config({ DECIMAL_PLACES: 10 })
// Create another BigNumber constructor, optionally passing in a configuration object
BN = BigNumber.clone({ DECIMAL_PLACES: 5 })
x = new BigNumber(1)
y = new BN(1)
x.div(3) // '0.3333333333'
y.div(3) // '0.33333'
网友评论