如何舍入 swift 中的 NSDecimalNumber?
How to round an NSDecimalNumber in swift?
我找不到这方面的任何资源,我一直在尝试各种方法,但没有任何效果。
根据 Apple 的文档,您可以像这样舍入 NSDecimalNumber:
NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior: NSDecimalNumberBehaviors?#>)
它采用 NSDecimalNumberBehavior,我不确定如何操作它,因为它 (1) 无法启动到变量中并更改其属性,并且 (2) 文档中的 roundingMode() 方法没有'取任何参数,但是Xcode为"self"填了一个参数space。
我完全迷失了。回到最基本的问题;如何在 swift 中舍入 NSDecimalNumber?
提前致谢
你可以这样做
let x = 5
let y = 2
let total = x.decimalNumberByDividingBy(y).decimalNumberByRoundingAccordingToBehavior( NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundUp, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false))
NSDecimalNumberBehaviors
是一个 协议 ,因此无法实例化。你需要一个符合协议的class对象。苹果为此提供了 class NSDecimalNumberHandler
,例如:
let handler = NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundBankers, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
let rounded = dec.decimalNumberByRoundingAccordingToBehavior(handler)
scale
参数是您想要的小数位数,即 0
四舍五入为整数。
// get a decimal num from a string
let num = NSDecimalNumber.init(string: numStr)
// create an NSDecimalNumberHandler instance
let behaviour = NSDecimalNumberHandler(roundingMode:.RoundUp,
scale: 1, raiseOnExactness: false,
raiseOnOverflow: false, raiseOnUnderflow:
false, raiseOnDivideByZero: false)
// pass the handler to the method decimalNumberByRoundingAccordingToBehaviour.
// This is an instance method on NSDecimalNumber that takes an object that
// conforms to the protocol NSDecimalNumberBehaviors, which NSDecimalNumberHandler does!
let numRounded = num.decimalNumberByRoundingAccordingToBehavior(behaviour)
我找不到这方面的任何资源,我一直在尝试各种方法,但没有任何效果。
根据 Apple 的文档,您可以像这样舍入 NSDecimalNumber:
NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior: NSDecimalNumberBehaviors?#>)
它采用 NSDecimalNumberBehavior,我不确定如何操作它,因为它 (1) 无法启动到变量中并更改其属性,并且 (2) 文档中的 roundingMode() 方法没有'取任何参数,但是Xcode为"self"填了一个参数space。
我完全迷失了。回到最基本的问题;如何在 swift 中舍入 NSDecimalNumber?
提前致谢
你可以这样做
let x = 5
let y = 2
let total = x.decimalNumberByDividingBy(y).decimalNumberByRoundingAccordingToBehavior( NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundUp, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false))
NSDecimalNumberBehaviors
是一个 协议 ,因此无法实例化。你需要一个符合协议的class对象。苹果为此提供了 class NSDecimalNumberHandler
,例如:
let handler = NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundBankers, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
let rounded = dec.decimalNumberByRoundingAccordingToBehavior(handler)
scale
参数是您想要的小数位数,即 0
四舍五入为整数。
// get a decimal num from a string
let num = NSDecimalNumber.init(string: numStr)
// create an NSDecimalNumberHandler instance
let behaviour = NSDecimalNumberHandler(roundingMode:.RoundUp,
scale: 1, raiseOnExactness: false,
raiseOnOverflow: false, raiseOnUnderflow:
false, raiseOnDivideByZero: false)
// pass the handler to the method decimalNumberByRoundingAccordingToBehaviour.
// This is an instance method on NSDecimalNumber that takes an object that
// conforms to the protocol NSDecimalNumberBehaviors, which NSDecimalNumberHandler does!
let numRounded = num.decimalNumberByRoundingAccordingToBehavior(behaviour)