安裝
npm install decimal.js
計算
const a = 9.99;
const b = 8.03;
// 加法
let c = new Decimal(a).add(new Decimal(b))
// 減法
let d = new Decimal(a).sub(new Decimal(b))
// 乘法
let e = new Decimal(a).mul(new Decimal(b))
// 除法
let f = new Decimal(a).div(new Decimal(b))
var a = moment(this.endMonth);
var b = moment(this.startingPaymentMonth);
const monthNum = a.diff(b, 'months') + 1;
this.total = new Decimal(monthNum).mul(new Decimal(this.unitPrice)).mul(new Decimal(this.area)).toFixed(2, Decimal.ROUND_FLOOR);
保留位數(shù)及取整
// 保留幾位小數(shù) , 跟 js 中的 number 一樣
toFixed
x = 3.456
// 向下取整
x.toFixed(2, Decimal.ROUND_DOWN) // '3.45' (舍入模式 向上0 向下1 四舍五入 4,7)
// 向上取整
Decimal.ROUND_UP
//四舍五入
ROUND_HALF_UP