NSDecimalNumber 精确数据处理

一、NSDecimalNumber使用场景

  • 四舍五入
  • 商品价格处理

二、NSDecimalNumber使用

1.四舍五入

NSDecimalNumber * inputNumber = [[NSDecimalNumber alloc]initWithString:@"1.45"];
NSDecimalNumberHandler * hander = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundPlain scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber * outputNumber = [inputNumber decimalNumberByRoundingAccordingToBehavior:hander]; NSLog(@"%@",outputNumber);

控制台输出

2016-07-13 15:35:35.517 Demo[27881:309225] 1.5

解释下函数

+ (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode
                                               scale:(short)scale
                                    raiseOnExactness:(BOOL)raiseOnExactness
                                     raiseOnOverflow:(BOOL)raiseOnOverflow
                                    raiseOnUnderflow:(BOOL)raiseOnUnderflow
                                 raiseOnDivideByZero:(BOOL)raiseOnDivideByZero
* roundingMode
/*
typedef NS_ENUM(NSUInteger, NSRoundingMode) {
    NSRoundPlain,   // Round up on a tie(四舍五入)
    NSRoundDown,    // Always down == truncate(只舍不入)
    NSRoundUp,      // Always up(只入不舍)
    NSRoundBankers  // on a tie round so last digit is even(也是四舍五入,这是和NSRoundPlain不一样,如果精确的哪位是5,
它要看精确度的前一位是偶数还是奇数,如果是奇数,则入,偶数则舍,例如scale=1,表示精确到小数点后一位, NSDecimalNumber 为1.25时,
NSRoundPlain结果为1.3,而NSRoundBankers则是1.2),下面是例子:
};
*/
* scale表示精确到小数点后几位,并且NSRoundingMode影响的也是scale位
* raiseOnExactness/raiseOnOverflow/raiseOnUnderflow raiseOnDivideByZero
分别表示数据精确/上溢/下益/除以0时是否以异常处理,一般情况下都设置为NO
NSDecimalNumber * inputNumber = [[NSDecimalNumber alloc]initWithString:@"1.25"];
NSDecimalNumberHandler * plainHander = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundPlain scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumberHandler * bankersHander = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundBankers scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

NSDecimalNumber * plainHanderNumber = [inputNumber decimalNumberByRoundingAccordingToBehavior:plainHander];
NSDecimalNumber * bankersHanderNumber = [inputNumber decimalNumberByRoundingAccordingToBehavior:bankersHander];
NSLog(@"plainHanderNumber %@; bankersHanderNumber %@",plainHanderNumber,bankersHanderNumber);
输出为
2016-07-13 16:41:02.062 Demo[31970:363504] plainHanderNumber 1.3; bankersHanderNumber 1.2
下面是苹果官方给出的各个枚举值,得到的结果,精确到小数点后一位
/***************    Type definitions        ***********/

// Rounding policies :
// Original
//    value 1.2  1.21  1.25  1.35  1.27
// Plain    1.2  1.2   1.3   1.4   1.3
// Down     1.2  1.2   1.2   1.3   1.2
// Up       1.2  1.3   1.3   1.4   1.3
// Bankers  1.2  1.2   1.2   1.4   1.3

2.商品价格处理

  • NSDecimalNumber的主要工作是提供定替代C的算术运算。下面是NSDecimalNumber算术的方法说明
NSDecimalNumber *price1 = [NSDecimalNumber decimalNumberWithString:@"15.99"];
NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"29.99"];
NSDecimalNumber *coupon = [NSDecimalNumber decimalNumberWithString:@"5.00"];
NSDecimalNumber *discount = [NSDecimalNumber decimalNumberWithString:@".90"];
NSDecimalNumber *numProducts = [NSDecimalNumber decimalNumberWithString:@"2.0"];

NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];
NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon];
NSDecimalNumber *afterDiscount = [afterCoupon decimalNumberByMultiplyingBy:discount];
NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
NSDecimalNumber *averageSquared = [average decimalNumberByRaisingToPower:2];

NSLog(@"Subtotal: %@", subtotal);                    // 45.98
NSLog(@"After coupon: %@", afterCoupon);           // 40.98
NSLog((@"After discount: %@"), afterDiscount);       // 36.882
NSLog(@"Average price per product: %@", average);    // 18.441
NSLog(@"Average price squared: %@", averageSquared); // 340.070481

通常货币计算都是保存小数点后两位,只要这样

        NSDecimalNumber * inputNumber = [[NSDecimalNumber alloc]initWithString:@"340.070481"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
                                   decimalNumberHandlerWithRoundingMode:NSRoundUp
                                   scale:2
                                   raiseOnExactness:NO
                                   raiseOnOverflow:NO
                                   raiseOnUnderflow:NO
                                   raiseOnDivideByZero:YES];
NSDecimalNumber * number = [inputNumber decimalNumberByRoundingAccordingToBehavior: roundUp];
结果就是340.07

你可能感兴趣的:(NSDecimalNumber 精确数据处理)