xcode 如何快速舍入 NSDecimalNumber?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31070831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to round an NSDecimalNumber in swift?
提问by Naldhelaan
I can't find any resources on this, and I've been trying all sorts of stuff, but nothing works.
我找不到这方面的任何资源,我一直在尝试各种东西,但没有任何效果。
According to Apple's documentation, you round an NSDecimalNumber like this:
根据 Apple 的文档,您可以像这样对 NSDecimalNumber 进行四舍五入:
NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior: NSDecimalNumberBehaviors?#>)
It takes in an NSDecimalNumberBehavior, which I'm unsure how to manipulate since it (1) cannot be initiated into a variable and have it's properties changed, and (2) the roundingMode() method according to the documentation doesn't take any parameters, but Xcode fills in a parameter space for "self".
它接受一个 NSDecimalNumberBehavior,我不确定如何操作它,因为它 (1) 无法初始化为变量并更改其属性,并且 (2) 根据文档的 roundingMode() 方法不接受任何参数,但 Xcode 为“self”填充了一个参数空间。
I'm totally lost on this. Back to the basic question; How can I round an NSDecimalNumber in swift?
我完全迷失了这一点。回到基本问题;如何快速舍入 NSDecimalNumber?
Thanks in advance
提前致谢
采纳答案by ?zgür Ersil
you can do it like that
你可以这样做
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))
回答by Arkku
NSDecimalNumberBehaviors
is a protocoland thus cannot be instantiated. You need an object of a classconforming to the protocol. Apple provides the class NSDecimalNumberHandler
for this purpose, e.g.:
NSDecimalNumberBehaviors
是一个协议,因此不能被实例化。您需要一个符合协议的类的对象。AppleNSDecimalNumberHandler
为此目的提供了该类,例如:
let handler = NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundBankers, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
let rounded = dec.decimalNumberByRoundingAccordingToBehavior(handler)
The scale
argument is the number of decimals you want, i.e., 0
rounds to an integer.
该scale
参数是要小数,即数量0
回合的整数。
回答by smileBot
// 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)