iOS高精度数值计算

对于电商或者金融类app,使用float、double来计算价格数值可能是达不到要求的。使用apple官方API:NSDecimalNumber可能是更好的选择。

  • 初始化

- (instancetype)initWithMantissa:(unsigned long long)mantissa exponent:(short)exponent isNegative:(BOOL)flag;
- (instancetype)initWithDecimal:(NSDecimal)dcm;
- (instancetype)initWithString:(nullable NSString *)numberValue;
- (instancetype)initWithString:(nullable NSString *)numberValue locale:(nullable id)locale;


实例:
//mantissa:长整形;exponent:指数;flag:正负数          
NSDecimalNumber * amount0 = [[NSDecimalNumber alloc] initWithMantissa:42 exponent:-2 isNegative:NO];//0.42  
NSDecimalNumber * amount1 = [[NSDecimalNumber alloc] initWithMantissa:42 exponent:2 isNegative:NO];//-4200 

//locale代表一种格式,对于这种格式可以参考一下例子去理解  
NSDictionary *locale0 = [NSDictionary dictionaryWithObject:@"," forKey:NSLocaleDecimalSeparator];    //以","当做小数点格式  
NSDecimalNumber * amount2 = [[NSDecimalNumber alloc] initWithString:@"42,68" locale:locale0];//42.68  
NSLocale *locale5 = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];//法国数据格式,法国的小数点是','逗号  
NSDecimalNumber * amount3 = [[NSDecimalNumber alloc] initWithString:@"42.68" locale:locale5];//42.68  
  • 基本的算术方法
加法
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
减法
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
乘法
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
除法
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
幂次方
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power;
指数
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power;

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

+ (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;


实例:
// 四舍五入策略 :
// 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
roundingMode 要使用的舍入模式,有四种值: NSRoundUp, NSRoundDown, NSRoundPlain, and NSRoundBankers
scale 结果保留几位小数
aiseOnExactness 发生精确错误时是否抛出异常,一般为NO
raiseOnOverflow 发生溢出错误时是否抛出异常,一般为NO
raiseOnUnderflow 发生不足错误时是否抛出异常,一般为NO
raiseOnDivideByZero 被0除时是否抛出异常,一般为YES

NSDecimalNumberHandler * rounUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];  
NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:4284 exponent:-2 isNegative:NO];  
NSDecimalNumber * roundUpAmount = [subtotalAmount decimalNumberByRoundingAccordingToBehavior:rounUp]; 
  • 比较运算

- (NSComparisonResult)compare:(NSNumber *)decimalNumber;

NSDecimalNumber *count0 = [NSDecimalNumber decimalNumberWithString:@"41"];  
NSDecimalNumber *count1 = [NSDecimalNumber decimalNumberWithString:@"43"];  
NSComparisonResult result = [count0 compare:count1]; 
// 上升 = NSOrderedAscending  1、2、3、。。。。。
if (result == NSOrderedAscending) {  
      //41 < 43  
} else if (result == NSOrderedSame) {  
      //41 == 43  
} else if (result == NSOrderedDescending) {  
      //41 > 43  
}  


参考
iOS开发中高精度数值(货币)计算

你可能感兴趣的:(iOS高精度数值计算)