NSDecimalNumber用于精度准确的计算

Posted ~~Sharp~~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSDecimalNumber用于精度准确的计算相关的知识,希望对你有一定的参考价值。

在处理金额计算时,往往会涉及到小数,由于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用于精度准确的计算的主要内容,如果未能解决你的问题,请参考以下文章

NSDecimalNumber 值的比例和精度

iOS - 数值精度处理之NSDecimalNumber

如何在不损失精度的情况下解码 NSDecimalNumber?

计算R中混淆矩阵的准确度和精度

舍入 NSDecimalNumber

2016-5-25NSDecimalNumber 知识点的简单整理