大家都会在开发中遇到, 处理一个浮点型的数据时, 有时候会有一个精度的丢失, 造成数据不准确. float的精度为6-7位有效数字,double的精度为15-16位,所以在处理浮点型数据时尽量选择double类型。但是在一些涉及数据计算,大小比较时责任重大,非常容易出错,因此使用系统自带的NSDecimalNumber做处理, 能解决这问题。
1.NSDecimalNumber
NSDecimalNumber是NSNumber的不可变子类。苹果针对浮点型计算时存在精度计算误差的问题而提供的一个计算类,它是基于10进制的定点计算保证了精度不会缺失。同时也可以定制精度的取正类型:向上取正、向下去正、四舍五入等。相对与浮点类型的计算,NSDecimalNumber提供了更加精准的计算。
2.NSDecimalNumber 基本运算
//1、字符串 -> NSDecimalNumber
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:@"123"];
//2、NSNumber -> NSDecimalNumber(基础类型的话,先转成NSNumber)
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInt:123] decimalValue]];
//相关运算
/* +
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
*/
NSDecimalNumber *num3 = [num1 decimalNumberByAdding:num2];
/*减 -
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
*/
NSDecimalNumber *num4 = [num1 decimalNumberBySubtracting:num2];
/*乘 *
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
*/
NSDecimalNumber *num5 = [num1 decimalNumberByMultiplyingBy:num2];
/*除 \
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;//带保留小数位数
*/
NSDecimalNumber *num6 = [num1 decimalNumberByDividingBy:num2];
/*比较
- (NSComparisonResult)compare:(NSNumber *)decimalNumber;
*/
NSComparisonResult result = [num1 compare:num2];
NSLog(@"result == %ld",(long)result);
//保留小数及相关参数含义
/*保留小数
+ (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;
*/
// 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 (特殊的四舍五入,碰到保留位数后一位的数字为5时,根据前一位的奇偶性决定。为偶时向下取正,为奇数时向上取正。如:1.25保留1为小数。5之前是2偶数向下取正1.2;1.35保留1位小数时。5之前为3奇数,向上取正1.4)
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
};
//scale:保留有效小数的个数(为0的无效小数后自动过滤).
//Exactness:进度异常、Overflow:向上溢出、Underflow:向下溢出、DivideByZero:除数为0。当参数为YES出错会
抛出异常,为NO时忽略异常。返回nil.
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundUp
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
所以在处理有关浮点型数据,后台传字符串的格式,防止丢失精度。