Js处理小数


decimal.js 官网地址 https://mikemcl.github.io/decimal.js/

比较大小用 lt lte

lessThan.lt(x) ⇒ boolean
x: number|string|Decimal
Returns true if the value of this Decimal is less than the value of x, otherwise returns false.
Note: This method uses the cmp method internally.

(0.3 - 0.2) < 0.1                        // true
x = new Decimal(0.3).minus(0.2)
x.lessThan(0.1)                          // false
new Decimal(0).lt(x)                     // true

lessThanOrEqualTo.lte(x) ⇒ boolean
x: number|string|Decimal
Returns true if the value of this Decimal is less than or equal to the value of x, otherwise returns false.
Note: This method uses the cmp method internally.


0.1 <= (0.3 - 0.2)                              // false
x = new Decimal(0.1)
x.lessThanOrEqualTo(Decimal(0.3).minus(0.2))    // true
new Decimal(-1).lte(x)                          // true

评论