场景:
我们在做金钱计算的时候会遇到一个问题,就是精确的问题,许多项目尤其是电商类项目都会涉及到,就是当我们对价格用float或者double进行处理的时候,有的时候总是会出现一些误差,还有就是假如我们精确小数点后面两位,就有可能出现xxx.00或者xx.x0的问题,这些都是我们在处理价格的时候可能会遇到的坑!!而且涉及到钱的问题,不管什么app都要做到特别的精确,不能出现任何的误差!
NSDecimalNumber简介
顾名思义这是一个十进制数字类,继承自NSNumber,苹果针对浮点类型计算精度问题提供出来的计算类,基于十进制的科学计数法来计算,同时可以指定舍入模式,一般用于货币计算。
非静态初始化方法
/**
mantissa:长整形;
exponent:指数;
flag:正负数。
**/
- (instancetype)initWithMantissa:(unsigned long long)mantissa exponent:(short)exponent isNegative:(BOOL)flag;
例子:
NSDecimalNumber *firstNumber = [[NSDecimalNumber alloc] initWithMantissa:1275 exponent:-3 isNegative:NO];//1.275
/**
NSDecimal是一个结构体
typedef struct {
signed int _exponent:8;
unsigned int _length:4; // length == 0 && isNegative -> NaN
unsigned int _isNegative:1;
unsigned int _isCompact:1;
unsigned int _reserved:18;
unsigned short _mantissa[NSDecimalMaxSize];
} NSDecimal;
**/
- (instancetype)initWithDecimal:(NSDecimal)dcm NS_DESIGNATED_INITIALIZER;
例子:
NSDecimal asStruct = {2,1,0,0,1,8};
NSDecimalNumber *asNewObject = [[NSDecimalNumber alloc] initWithDecimal:asStruct];//800
/**
numericString:除了数字字符串,numericString可以包括一个“+”或“-”,一个“E”或“E”,用科学记数法表示一个数的指数
**/
- (instancetype)initWithString:(nullable NSString *)numberValue;
例子:
NSDecimalNumber *discountAmount = [[NSDecimalNumber alloc] initWithString:@"-25E3"];//-25000
/**
locale:一个NSLocaleDecimalSeparator划分的部分数的有效组成部分。
**/
- (instancetype)initWithString:(nullable NSString *)numberValue locale:(nullable id)locale;
例子:
NSDictionary *locale = @{NSLocaleDecimalSeparator :@","};//以","当做分隔符格式
NSDecimalNumber *discountAmount = [[NSDecimalNumber alloc] initWithString:@"123,40" locale:locale];//123.4
静态初始化方法
+ (NSDecimalNumber *)decimalNumberWithMantissa:(unsigned long long)mantissa exponent:(short)exponent isNegative:(BOOL)flag;
+ (NSDecimalNumber *)decimalNumberWithDecimal:(NSDecimal)dcm;
+ (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue;
+ (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue locale:(nullable id)locale;
加:
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.5"];
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSDecimalNumber *resultNumber = [oneNumber decimalNumberByAdding:twoNumber];//4.5
减:
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.5"];
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSDecimalNumber *resultNumber = [twoNumber decimalNumberBySubtracting:oneNumber];//1.5
乘:
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.5"];
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSDecimalNumber *resultNumber = [oneNumber decimalNumberByMultiplyingBy:twoNumber];//4.5
除:
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.5"];
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSDecimalNumber *resultNumber = [twoNumber decimalNumberByDividingBy:oneNumber];//2
a的n次方:
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power;
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSDecimalNumber *resultNumber = [twoNumber decimalNumberByRaisingToPower:3];//27
指数运算:
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power;
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSDecimalNumber *resultNumber = [twoNumber decimalNumberByMultiplyingByPowerOf10:3];//3000
四舍五入运算:
//behavior:NSDecimalNumberHandler对象
- (NSDecimalNumber *)decimalNumberByRoundingAccordingToBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
例子:
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.5"];
NSDecimalNumber *resultNumber = [oneNumber decimalNumberByRoundingAccordingToBehavior:nil];//2
比较运算:
-(NSComparisonResult)compare:(NSNumber *)decimalNumber;
例子1:
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.5"];
NSDecimalNumber *twoNumber = [NSDecimalNumber decimalNumberWithString:@"3"];
NSComparisonResult comparisonResult = [oneNumber compare:@(twoNumber.floatValue)];//NSOrderedAscending
例子2:
NSDecimal discount1 = [[NSDecimalNumber decimalNumberWithString:@".85"] decimalValue];
NSDecimal discount2 = [[NSDecimalNumber decimalNumberWithString:@".9"] decimalValue];
NSComparisonResult result = NSDecimalCompare(&discount1, &discount2);//NSOrderedAscending
NSDecimalNumberHandler
这是一个NSDecimalNumber的公共协议处理类,可以设置舍入模式以及计算错误的处理;配合NSDecimalNumber来使用,将这个类的实例当做NSDecimalNumber相应API的参数来控制数字处理的结果。
/**
初始化方法
@param roundingMode 舍入方式
@param scale 小数点后舍入值的位数。
@param exact 精度错误处理;YES:如果出现错误,将引发异常,NO:忽略错误并将控制权放回给调用者。
@param overflow 溢出错误处理;YES:如果出现错误,将引发异常,NO:忽略错误并将控制权放回给调用者。
@param underflow 下溢错误处理;YES:如果出现错误,将引发异常,NO:忽略错误并将控制权放回给调用者。
@param divideByZero 除以0的错误处理;YES:如果出现错误,将引发异常,NO:忽略错误并将控制权放回给调用者。
@return NSDecimalNumberHandler对象
*/
+ (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode
scale:(short)scale
raiseOnExactness:(BOOL)exact
raiseOnOverflow:(BOOL)overflow
raiseOnUnderflow:(BOOL)underflow
raiseOnDivideByZero:(BOOL)divideByZero
/**
快速构造方法
roundingMode:NSRoundPlain
scale:全精度
exact:NO
overflow:NO
underflow:NO
divideByZero:NO
*/
+ (NSDecimalNumberHandler *)defaultDecimalNumberHandler;
NSRoundingMode的几种方式:
- NSRoundPlain:四舍五入
- NSRoundDown:只舍不入
- NSRoundUp:只入不舍
- NSRoundBankers: 在四舍五入的基础上加了一个判断:当最后一位为5的时候,只会舍入成偶数。比如:1.25不会返回1.3而是1.2,因为1.3不是偶数。
NSDecimalNumber *oneNumber = [NSDecimalNumber decimalNumberWithString:@"1.567"];
NSDecimalNumberHandler *handlerTwo = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
NSDecimalNumber *resultNumber = [oneNumber decimalNumberByRoundingAccordingToBehavior:handlerTwo];//1.57