iOS NSDecimalNumber
2017-07-14 本文已影响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)