Rounding NSDecimalNumber

后端 未结 4 520
深忆病人
深忆病人 2020-12-10 00:54

I\'m having the hardest time figuring out something that seems like it should be very simple. I need to accurately round an NSDecimalNumber to a particular number of decimal

相关标签:
4条回答
  • 2020-12-10 01:39

    For those that prefer example code...

    To round to 2 decimal places (12345.68):

    NSDecimalNumber *originalNumber = [NSDecimalNumber decimalNumberWithString:@"12345.6789"];
    NSDecimalNumberHandler *behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
                                                                                              scale:2
                                                                                   raiseOnExactness:NO
                                                                                    raiseOnOverflow:NO
                                                                                   raiseOnUnderflow:NO
                                                                                raiseOnDivideByZero:NO];
    
    NSDecimalNumber *roundedNumber = [originalNumber decimalNumberByRoundingAccordingToBehavior:behavior];
    

    To round to the nearest thousand (12000):

    NSDecimalNumber *originalNumber = [NSDecimalNumber decimalNumberWithString:@"12345.6789"];
    NSDecimalNumberHandler *behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
                                                                                              scale:-3
                                                                                   raiseOnExactness:NO
                                                                                    raiseOnOverflow:NO
                                                                                   raiseOnUnderflow:NO
                                                                                raiseOnDivideByZero:NO];
    
    NSDecimalNumber *roundedNumber = [originalNumber decimalNumberByRoundingAccordingToBehavior:behavior];
    
    0 讨论(0)
  • 2020-12-10 01:40

    I got it working using the below code in Swift 3.

    let amount = NSDecimalNumber(string: "123.456789")
    let handler = NSDecimalNumberHandler(roundingMode: .plain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
    let roundedAmount = amount.rounding(accordingToBehavior: handler)
    

    Note the scale parameter, used to define the decimal places you need. Outlined here: https://developer.apple.com/reference/foundation/nsdecimalnumberhandler/1578295-decimalnumberhandlerwithrounding

    0 讨论(0)
  • 2020-12-10 01:45

    I'm using this solution:

    import Foundation
    
    extension NSDecimalNumber {
        public func round(_ decimals:Int) -> NSDecimalNumber {
            return self.rounding(accordingToBehavior:
                NSDecimalNumberHandler(roundingMode: .plain,
                                       scale: Int16(decimals),
                                       raiseOnExactness: false,
                                       raiseOnOverflow: false,
                                       raiseOnUnderflow: false,
                                       raiseOnDivideByZero: false))
        }
    }
    
    let amount = NSDecimalNumber(string: "123.456")
    
    amount.round(2)  --> 123.46
    amount.round(1)  --> 123.5
    amount.round(0)  --> 123
    amount.round(-1) --> 120
    amount.round(-2) --> 100
    
    0 讨论(0)
  • 2020-12-10 01:53

    You simply call decimalNumberByRoundingAccordingToBehavior:with the desired NSDecimalNumberBehaviors protocol. See the NSDecimalNumberBehaviors reference in the dev docs.

    Update: See http://www.cimgf.com/2008/04/23/cocoa-tutorial-dont-be-lazy-with-nsdecimalnumber-like-me/

    0 讨论(0)
提交回复
热议问题