How to round an NSDecimalNumber in swift?
Asked Answered
G

3

12

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:

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".

I'm totally lost on this. Back to the basic question; How can I round an NSDecimalNumber in swift?

Thanks in advance

Gupta answered 26/6, 2015 at 10:25 Comment(1)
NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior: is not a class method. This means that you can call on a decimal number instance, to obtain its rounded value.Detached
O
11

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))   
Oblong answered 26/6, 2015 at 10:31 Comment(2)
Like string manipulation and other things in Swift, is anyone else perturbed by how much code is required to simply do this?Braddy
This is obviously not the correct answer. Why is this the accepted answer? The question was asking about NSDecimalNumber, not about dividing Ints.Antagonism
C
9

NSDecimalNumberBehaviors is a protocol and thus cannot be instantiated. You need an object of a class conforming to the protocol. Apple provides the class NSDecimalNumberHandler for this purpose, e.g.:

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.

Cornwall answered 26/6, 2015 at 10:31 Comment(2)
Using the defaultDecimalNumberHandler doesn't seem to make any rounding. Can you provide an example?Gupta
@Gupta My bad regarding the default handler, see example for how to construct your own.Cornwall
K
2
// 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)
Kirman answered 12/10, 2015 at 3:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.