NSDecimalNumber简介
苹果针对浮点类型计算精度问题提供出来的计算类,基于十进制的科学计数法来计算,同时可以指定舍入模式,一般用于货币计算。中国货币最小流通单位是分,也就是小数点后两位。
NSDecimalNumberHandler
/**
初始化方法
@param roundingMode 舍入方式
@param scale 小数点后舍入值的位数
@param exact 精度错误处理
@param overflow 溢出错误处理
@param underflow 下溢错误处理
@param divideByZero 除以0的错误处理
@return NSDecimalNumberHandler对象
*/
+ (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;
NSRoundingMode 的类型
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
};
NSRoundPlain 是四舍五入
NSRoundDown 是向下取整
NSRoundUp 是向上取整
NSRoundBankers 是在四舍五入的基础上,加上末尾数为5时,变成偶数的规则
一:四舍五入
// 四舍五入
NSDecimalNumberHandler *roundPlain = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
{
NSString *addStr = @"1.0";
NSString *oneStr = @"1.114";
NSString *twoStr = @"1.116";
NSDecimalNumber *oneNum = [NSDecimalNumber decimalNumberWithString:oneStr];
NSDecimalNumber *twoNum = [NSDecimalNumber decimalNumberWithString:twoStr];
NSDecimalNumber *addNum = [NSDecimalNumber decimalNumberWithString:addStr];
// 加法
oneNum = [oneNum decimalNumberByAdding:addNum withBehavior:roundPlain];
twoNum = [twoNum decimalNumberByAdding:addNum withBehavior:roundPlain];
NSLog(@"\n1.114 + 1.0 (保留两位小数)四舍五入的结果为:%@\n1.116 + 1.0 (保留两位小数)四舍五入的结果为:%@", oneNum, twoNum);
}
二:向下取整
// 向下取整
NSDecimalNumberHandler *roundDown = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
{
NSString *subtractStr = @"1.0";
NSString *oneStr = @"1.114";
NSString *twoStr = @"1.116";
NSDecimalNumber *oneNum = [NSDecimalNumber decimalNumberWithString:oneStr];
NSDecimalNumber *twoNum = [NSDecimalNumber decimalNumberWithString:twoStr];
NSDecimalNumber *subtractNum = [NSDecimalNumber decimalNumberWithString:subtractStr];
// 减法
oneNum = [oneNum decimalNumberBySubtracting:subtractNum withBehavior:roundDown];
twoNum = [twoNum decimalNumberBySubtracting:subtractNum withBehavior:roundDown];
NSLog(@"\n1.114 - 1.0 (保留两位小数)向下取整的结果为:%@\n1.116 - 1.0 (保留两位小数)向下取整的结果为:%@", oneNum, twoNum);
}
三:向上取整
// 向上取整
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundUp scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
{
NSString *multiplyStr = @"1.0";
NSString *oneStr = @"1.114";
NSString *twoStr = @"1.116";
NSDecimalNumber *oneNum = [NSDecimalNumber decimalNumberWithString:oneStr];
NSDecimalNumber *twoNum = [NSDecimalNumber decimalNumberWithString:twoStr];
NSDecimalNumber *multiplyNum = [NSDecimalNumber decimalNumberWithString:multiplyStr];
// 乘法
oneNum = [oneNum decimalNumberByMultiplyingBy:multiplyNum withBehavior:roundUp];
twoNum = [twoNum decimalNumberByMultiplyingBy:multiplyNum withBehavior:roundUp];
NSLog(@"\n1.114 * 1.0 (保留两位小数)向上取整的结果为:%@\n1.116 * 1.0 (保留两位小数)向上取整的结果为:%@", oneNum, twoNum);
}
四:NSRoundBankers
// 在四舍五入的基础上,加上末尾数为5时,变成偶数的规则,例如小数点后两位, 1.215 时变为 1.22, 1.225 时变为 1.22
NSDecimalNumberHandler *roundBankers = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
{
NSString *dividStr = @"1.0";
NSString *oneStr = @"1.115";
NSString *twoStr = @"1.125";
NSDecimalNumber *oneNum = [NSDecimalNumber decimalNumberWithString:oneStr];
NSDecimalNumber *twoNum = [NSDecimalNumber decimalNumberWithString:twoStr];
NSDecimalNumber *dividNum = [NSDecimalNumber decimalNumberWithString:dividStr];
// 除法
oneNum = [oneNum decimalNumberByDividingBy:dividNum withBehavior:roundBankers];
twoNum = [twoNum decimalNumberByDividingBy:dividNum withBehavior:roundBankers];
NSLog(@"\n1.115 / 1.0 (保留两位小数)NSRoundBankers的结果为:%@\n1.125 / 1.0 (保留两位小数)NSRoundBankers的结果为:%@", oneNum, twoNum);
}