NSDecimalNumber是使用货币时最好的格式。
官方文档
NSDecimalNumber 是 NSNumber 的一个子类, 比 NSNumber 的功能更为强大, 四舍五入, 取整, 输入后自动去掉数值前面无用的 0 等等. 由于 NSDecimalNumber 精度较高, 所以会比基本数据类型费时, 所以需要权衡考虑, 苹果官方建议在货币以及要求精度很高的场景下使用.
通常情况下我们会使用 NSDecimalNumberHandler 这个格式化器对其需要约束的格式进行设置
NSDecimalNumberHandler
let decHandler:NSDecimalNumberHandler = NSDecimalNumberHandler.init(roundingMode: .plain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
roundingMode 文档是一个枚举类型
public enum RoundingMode : UInt {
case plain = 0 //只入不舍
case down = 1//只舍不入
case up = 2//四舍五入
case bankers = 3//四舍六入, 中间值时, 取最近的,保持保留最后一位为偶数
}
scale: 保留几位小数
raiseOnExactness: 发生精确错误时是否抛出异常,一般为false
raiseOnOverflow: 发生溢出错误时是否抛出异常,一般为false
raiseOnUnderflow: 发生不足错误时是否抛出异常,一般为false
raiseOnDivideByZero: 除数是0时是否抛出异常,一般为true
补零问题
NSDecimalNumber 做精度限制, 如果小数点后面是以0结尾, 则不会显示,如1024.00 保留2位小数 结果为:1024
前端为了显示效果需要补零操作
let decNumber = NSDecimalNumber.init(string: "1024")
let decHandler:NSDecimalNumberHandler = NSDecimalNumberHandler.init(roundingMode: .plain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
let dNumber = decNumber.rounding(accordingToBehavior: decHandler)
let p = 2
let count : Int = (p < 0 ? 0 : p)
var formatterString : String = "0."
if count == 0 {
formatterString = "0"
}
for _ in 0 ..< count {
formatterString.append("0")
}
let formatter : NumberFormatter = NumberFormatter()
formatter.positiveFormat = formatterString
formatter.string(from: dNumber)!
完整方法
func precisionString(with number:String, precision: String , isRound: Bool = false) -> String {
guard isPurnFloat(string: number) && isPurnFloat(string: precision) else {
return number
}
guard conerData(precision, .MyInt).0! >= 0 else {
return number
}
let decNumber = NSDecimalNumber.init(string: number)
var decHandler:NSDecimalNumberHandler?
if isRound {
decHandler = NSDecimalNumberHandler.init(roundingMode: .plain, scale: Int16(precision)!, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
}else{
if conerData(number, .MyDouble).2! > 0 {
decHandler = NSDecimalNumberHandler.init(roundingMode: .up, scale: Int16(precision)!, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
}else{
decHandler = NSDecimalNumberHandler.init(roundingMode: .up, scale: Int16(precision)!, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
}
}
let dNumber = decNumber.rounding(accordingToBehavior: decHandler)
let p = conerData(precision, .MyInt).0!
let count : Int = (p < 0 ? 0 : p)
var formatterString : String = "0."
if count == 0 {
formatterString = "0"
}
for _ in 0 ..< count {
formatterString.append("0")
}
let formatter : NumberFormatter = NumberFormatter()
formatter.positiveFormat = formatterString
return formatter.string(from: dNumber)!
}
//MARK:- 是否数字
public func isPurnFloat(string: String) -> Bool {
let scan: Scanner = Scanner(string: string)
var val:Double = 0
return scan.scanDouble(&val) && scan.isAtEnd
}
NSDecimalNumber运算
NSDecimalNumber:是苹果针对浮点型计算时存在精度误差的问题而提供的一个类
let num1 = NSDecimalNumber.init(string: "1024.1024")
let num2 = NSDecimalNumber.init(string: "1024.1024")
//加
String(format: "%@", num1.adding(num2))
//减
String(format: "%@", num1.subtracting(num2))
//乘
String(format: "%@", num1.multiplying(num2))
//除
String(format: "%@", num1.dividing(num2))
//n次方(2次方)
String(format: "%@", num1.raising(toPower: 2))
//指数
String(format: "%@", num1.multiplying(byPowerOf10: 2))
相关文档
open func adding(_ decimalNumber: NSDecimalNumber) -> NSDecimalNumber
open func adding(_ decimalNumber: NSDecimalNumber, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber
open func subtracting(_ decimalNumber: NSDecimalNumber) -> NSDecimalNumber
open func subtracting(_ decimalNumber: NSDecimalNumber, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber
open func multiplying(by decimalNumber: NSDecimalNumber) -> NSDecimalNumber
open func multiplying(by decimalNumber: NSDecimalNumber, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber
open func dividing(by decimalNumber: NSDecimalNumber) -> NSDecimalNumber
open func dividing(by decimalNumber: NSDecimalNumber, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber
open func raising(toPower power: Int) -> NSDecimalNumber
open func raising(toPower power: Int, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber
// compare two NSDecimalNumbers
open class var defaultBehavior: NSDecimalNumberBehaviors
// One behavior per thread - The default behavior is
// rounding mode: NSRoundPlain
// scale: No defined scale (full precision)
// ignore exactnessException
// raise on overflow, underflow and divide by zero.
open var objCType: UnsafePointer<CChar> { get }
// return 'd' for double
open var doubleValue: Double { get }
open func multiplying(byPowerOf10 power: Int16) -> NSDecimalNumber
open func multiplying(byPowerOf10 power: Int16, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber