美文网首页
iOS NSDecimalNumber

iOS NSDecimalNumber

作者: gaookey | 来源:发表于2020-11-09 14:50 被阅读0次
    基本运算
    let num = NSDecimalNumber(string: "35.89302")
    let num2 = NSDecimalNumber(string: "15.161")
    
    var result = NSDecimalNumber()
    
    // 加  51.05402
    result = num.adding(num2)
    // 减  20.73202
    result = num.subtracting(num2)
    // 乘  544.17407622
    result = num.multiplying(by: num2)
    // 除  2.36745729173537365609128685442912736626
    result = num.dividing(by: num2)
    // 次方  1288.3088847204
    result = num.raising(toPower: 2)
    

    设置小数点后保留位数
    /*
     NSDecimalNumber.RoundingMode :
     
     value    1.2  1.21  1.25  1.35  1.27
     
     plain    1.2  1.2   1.3   1.4   1.3
     down     1.2  1.2   1.2   1.3   1.2
     up       1.2  1.3   1.3   1.4   1.3
     bankers  1.2  1.2   1.2   1.4   1.3
     */
     
    let behavior = NSDecimalNumberHandler(roundingMode: .bankers,
                                          scale: 2, // 保留两位小数
                                          raiseOnExactness: false,
                                          raiseOnOverflow: false,
                                          raiseOnUnderflow: false,
                                          raiseOnDivideByZero: true)
    
    let num = NSDecimalNumber(string: "35.89302")
    let num2 = NSDecimalNumber(string: "15.161")
    var result = NSDecimalNumber()
    
    // 除  2.37
    result = num.dividing(by: num2, withBehavior: behavior)
    
    比较大小
    let num = NSDecimalNumber(string: "35.89302")
    let num2 = NSDecimalNumber(string: "15.161")
    
    // num > num2
    let result = num.compare(num2)
    
    if result == .orderedAscending {
        print("num < num2")
    } else if result == .orderedDescending {
        print("num > num2")
    } else if result == .orderedSame {
        print("num == num2")
    }
    

    相关文章

      网友评论

          本文标题:iOS NSDecimalNumber

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