decimal.js API

作者: Hyphone | 来源:发表于2018-07-10 14:35 被阅读0次

    获取绝对值

    a = Decimal.abs(x)
    b = new Decimal(x).abs()
    a.equals(b)                    // true
    
    x = new Decimal(-0.8)
    y = x.absoluteValue()         // '0.8'
    z = x.abs()                   // '0.8'
    

    获取整数

    a = Decimal.trunc(x)
    b = new Decimal(x).trunc()
    a.equals(b)                    // true
    
    x = new Decimal(123.456)
    x.truncated()                            // '123'
    y = new Decimal(-12.3)
    y.trunc()                                // '-12'
    

    获取正无穷大方向的整数

    a = Decimal.ceil(x)
    b = new Decimal(x).ceil()
    a.equals(b)                    // true
    
    x = new Decimal(1.3)
    x.ceil()                      // '2'
    y = new Decimal(-1.8)
    y.ceil()                      // '-1'
    

    获取负无穷大方向的整数

    a = Decimal.floor(x)
    b = new Decimal(x).floor()
    a.equals(b)                    // true
    
    x = new Decimal(1.8)
    x.floor()                   // '1'
    y = new Decimal(-1.3)
    y.floor()                   // '-2'
    

    获取最大值

    r = Decimal.max(x, y, z)
    

    获取最小值

    r = Decimal.min(x, y, z)
    

    获取大于等于0小于1的随机数

    Decimal.set({ precision: 10 })
    Decimal.random()                    // '0.4117936847'
    Decimal.random(20)                  // '0.78193327636914089009'
    

    获取小数位数

    x = new Decimal(1.234)
    x.decimalPlaces()              // '3'
    y = new Decimal(987.654321)
    y.dp()                         // '6'
    

    获取反值

    x = new Decimal(1.8)
    x.negated()                              // '-1.8'
    y = new Decimal(-1.3)
    y.neg()                                  // '1.3'
    

    获取位数

    x = new Decimal(1.234)
    x.precision()                            // '4'
    y = new Decimal(987000)
    y.sd()                                   // '3'
    y.sd(true)                               // '6'
    

    获取指定小数位数的字符串

    x = 3.456
    y = new Decimal(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, Decimal.ROUND_DOWN)  // '3.45'
    x.toFixed(5)                      // '3.45600'
    y.toFixed(5)                      // '3.45600'
    

    保留小数指定位数

    x = new Decimal(12.34567)
    x.toDecimalPlaces(0)                      // '12'
    x.toDecimalPlaces(1, Decimal.ROUND_UP)    // '12.3'
    
    y = new Decimal(9876.54321)
    y.toDP(3)                           // '9876.543'
    y.toDP(1, 0)                        // '9876.6'
    y.toDP(1, Decimal.ROUND_DOWN)       // '9876.5'
    

    舍入运算

    Decimal.set({ rounding: 4 })
    x = 1234.5
    x.round()                                // '1235'
    
    Decimal.rounding = Decimal.ROUND_DOWN
    x.round()                                // '1234'
    x                                        // '1234.5'
    

    %模运算

    a = Decimal.mod(x, y)
    b = new Decimal(x).mod(y)
    a.equals(b)                    // true
    
    1 % 0.9                                  // 0.09999999999999998
    x = new Decimal(1)
    x.modulo(0.9)                            // '0.1'
    
    y = new Decimal(8)
    z = new Decimal(-3)
    Decimal.modulo = 1
    y.mod(z)                                 // '2'
    Decimal.modulo = 3
    y.mod(z)                                 // '-1'
    

    加法

    a = Decimal.add(x, y)
    b = new Decimal(x).plus(y)
    a.equals(b)                    // true
    
    0.1 + 0.2                                // 0.30000000000000004
    x = new Decimal(0.1)
    y = x.plus(0.2)                          // '0.3'
    new Decimal(0.7).plus(x).plus(y)         // '1.1'
    

    减法

    a = Decimal.sub(x, y)
    b = new Decimal(x).sub(y)
    a.equals(b)                    // true
    
    0.3 - 0.1                                // 0.19999999999999998
    x = new Decimal(0.3)
    x.minus(0.1)                             // '0.2'
    

    乘法

    a = Decimal.mul(x, y)
    b = new Decimal(x).mul(y)
    a.equals(b)                    // true
    
    0.6 * 3                                  // 1.7999999999999998
    x = new Decimal(0.6)
    y = x.times(3)                           // '1.8'
    new Decimal('7e+500').times(y)           // '1.26e+501'
    

    除法

    a = Decimal.div(x, y)
    b = new Decimal(x).div(y)
    a.equals(b)                    // true
    
    x = new Decimal(355)
    y = new Decimal(113)
    x.dividedBy(y)             // '3.14159292035398230088'
    x.div(5)                   // '71'
    

    除法返回整数

    x = new Decimal(5)
    y = new Decimal(3)
    x.dividedToIntegerBy(y)     // '1'
    x.divToInt(0.7)             // '7'
    

    是否为Decimal实例

    a = new Decimal(1)
    b = {}
    a instanceof Decimal           // true
    Decimal.isDecimal(a)           // true
    Decimal.isDecimal(b)           // false
    

    是否相等

    0 === 1e-324                     // true
    x = new Decimal(0)
    x.equals('1e-324')               // false
    new Decimal(-0).eq(x)            // true  ( -0 === 0 )
    
    y = new Decimal(NaN)
    y.equals(NaN)                    // false
    

    是否大于

    0.1 > (0.3 - 0.2)                            // true
    x = new Decimal(0.1)
    x.greaterThan(Decimal(0.3).minus(0.2))       // false
    new Decimal(0).gt(x)                         // false
    

    是否大于或等于

    (0.3 - 0.2) >= 0.1                       // false
    x = new Decimal(0.3).minus(0.2)
    x.greaterThanOrEqualTo(0.1)              // true
    new Decimal(1).gte(x)                    // true
    

    是否小于

    (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
    

    是否小于或等于

    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
    

    是否为有限值

    x = new Decimal(1)
    x.isFinite()                             // true
    y = new Decimal(Infinity)
    y.isFinite()                             // false
    

    是否为整数

    x = new Decimal(1)
    x.isInteger()                            // true
    y = new Decimal(123.456)
    y.isInt()                                // false
    

    是否为数字

    x = new Decimal(NaN)
    x.isNaN()                                // true
    y = new Decimal('Infinity')
    y.isNaN()                                // false
    

    是否为负数

    x = new Decimal(-0)
    x.isNegative()                           // true
    y = new Decimal(2)
    y.isNeg                                  // false
    

    是否为正数

    x = new Decimal(0)
    x.isPositive()                           // true
    y = new Decimal(-2)
    y.isPos                                  // false
    

    是否为0

    x = new Decimal(-0)
    x.isZero() && x.isNeg()                  // true
    y = new Decimal(Infinity)
    y.isZero()                               // false
    

    相关文章

      网友评论

        本文标题:decimal.js API

        本文链接:https://www.haomeiwen.com/subject/kpupuftx.html