iOS NSDecimalNumber

作者: 莹莹张 | 来源:发表于2017-07-14 18:04 被阅读37次
    var total: NSDecimalNumber = NSDecimalNumber.zero
    let price1: NSDecimalNumber = 10
    let price2: NSDecimalNumber = 5
    total = price1.adding(price2)
    
    var price: NSDecimalNumber = NSDecimalNumber.zero
    let price1: NSDecimalNumber = 10
    let price2: NSDecimalNumber = 5
    price = price1.subtracting(price2)
    
    let price: NSDecimalNumber = 10
    let count: NSDecimalNumber = 5
    let total: NSDecimalNumber = price.multiplying(by: count)
    
    let total: NSDecimalNumber = 20
    let price: NSDecimalNumber = 5
    let count: NSDecimalNumber = total.dividing(price)
    
    • 四舍五入
    /*
    public enum RoundingMode : UInt {
            case plain // Round up on a tie
    
            case down // Always down == truncate
    
            case up // Always up
    
            case bankers // on a tie round so last digit is even
        }
    */
    let round = NSDecimalNumberHandler(roundingMode: NSDecimalNumber.RoundingMode.bankers, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
    let orginPrice: NSDecimalNumber = 999.9999
    let disCount: NSDecimalNumber = 0.75
    let price = orginPrice.multiplying(by: disCount, withBehavior:round)
    
    • 比较
    let price1: NSDecimalNumber = 10
    let price2: NSDecimalNumber = 5
    let result: NSComparisonResult  = price1.compare(price2)
    if result == NSOrderedAscending {
     } else if result == NSOrderedSame {
     } else if result == NSOrderedDescending {  
    }
    
    • 次方
    let count: NSDecimalNumber = 5
    let total: NSDecimalNumber = count.raising(toPower: 2)
    

    相关文章

      网友评论

      本文标题:iOS NSDecimalNumber

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