码迷,mamicode.com
首页 > 其他好文 > 详细

NSDecimalNumber用于精度准确的计算

时间:2016-08-31 20:20:17      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

在处理金额计算时,往往会涉及到小数,由于Double类型不准确,无法做到产品的要求。为了保证金额计算的准确性,建议使用NSDecimalNumber。

1.创建对象(常用的方法)

// mantissa:长整型数值;exponent:指数(几次方);flag:正负数。
+ (NSDecimalNumber *)decimalNumberWithMantissa:(unsigned long long)mantissa exponent:(short)exponent isNegative:(BOOL)flag;

NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithMantissa:829 exponent:-2 isNegative:NO];   //8.29
decimalNumber = [NSDecimalNumber decimalNumberWithMantissa:829 exponent:2 isNegative:YES]; //-82900

// locale代表一种格式
+ (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue locale:(nullable id)locale;
NSDictionary *locale = [NSDictionary dictionaryWithObject:@"," forKey:NSLocaleDecimalSeparator];    //以","当做小数点格式
NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"123,40" locale:locale];    //123.4

+ (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue;

 

2.逻辑运算

加:
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

减:
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

乘:
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

除:
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

a的n次方:
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power;
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

指数运算:
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power;
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

四舍五入运算:
- (NSDecimalNumber *)decimalNumberByRoundingAccordingToBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;

比较运算:
-(NSComparisonResult)compare:(NSNumber *)decimalNumber;

 

3.数值格式设置

// roundingMode:舍入模式; scale:保留几位小数;exact:发生精确错误时是否抛出异常,一般为NO;overflow:发生溢出错误时是否抛出异常,一般为NO; underflow:发生不足错误时是否抛出异常,一般为NO; divideByZero:被0除时是否抛出异常,一般为YES
+ (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;

    NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
                                       decimalNumberHandlerWithRoundingMode:NSRoundDown
                                       scale:2
                                       raiseOnExactness:NO
                                       raiseOnOverflow:NO
                                       raiseOnUnderflow:NO
                                       raiseOnDivideByZero:YES];

 

NSDecimalNumber用于精度准确的计算

标签:

原文地址:http://www.cnblogs.com/shpyoucan/p/5827019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!