iOS NSDecimalNumber精确数值计算以及小数点后精确保留2位数字
2015-12-05 15:33
429 查看
遇到的问题,从服务器拿的数据NSString类型的,然后转化成CGFloat类型的,数据的值就不准确了,没办法,就找了下资料,找到了苹果有专门的API,针对金融货币单位,希望可以解决有遇到同样问题的人.
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundBankers
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
枚举
NSRoundPlain, // Round up on a tie //貌似取整
NSRoundDown, // Always down == truncate //只舍不入
NSRoundUp, // Always up // 只入不舍
NSRoundBankers // on a tie round so last digit is even 貌似四舍五入
//90.7049+0.22 然后四舍五入
NSDecimalNumber *subtotal = [NSDecimalNumberdecimalNumberWithString:@"90.7049"];
NSDecimalNumber *discount = [NSDecimalNumberdecimalNumberWithString:@"0.22"];
NSDecimalNumber *total = [subtotal decimalNumberByAdding:discount withBehavior:roundUp];
NSLog(@"Rounded total: %@", total);
NSDecimalNumber*price1 = [NSDecimalNumberdecimalNumberWithString:@"15.99"];
NSDecimalNumber*price2 = [NSDecimalNumberdecimalNumberWithString:@"29.99"];
NSDecimalNumber*coupon = [NSDecimalNumberdecimalNumberWithString:@"5.00"];
NSDecimalNumber*discount = [NSDecimalNumberdecimalNumberWithString:@".90"];
NSDecimalNumber*numProducts = [NSDecimalNumberdecimalNumberWithString:@"2.0"];
NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];
NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon];
NSDecimalNumber *afterDiscount = [afterCoupon decimalNumberByMultiplyingBy:discount];
NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
NSDecimalNumber*averageSquared = [average decimalNumberByRaisingToPower:2];
的NSLog(@“小计:%@”,加);/ / 45.98
的NSLog(@“优惠券后:%@”,减);/ / 40.98
的NSLog((@“折后:%@”),乘);/ / 36.882
的NSLog(@“每股平均价产品:%@”,平均);/ / 18.441
的NSLog(@“平均价的平方:%@”,averageSquared);/ / 340.070481
//保留小数点后两位
NSDecimalNumberHandler*roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundUp
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
//9折后 36.882 有余进位结果 36.89
NSDecimalNumber*subtotal = [NSDecimalNumberdecimalNumberWithString:@"40.98"];
NSDecimalNumber*discount = [NSDecimalNumberdecimalNumberWithString:@".90"];
NSDecimalNumber*total = [subtotal decimalNumberByMultiplyingBy:discount
withBehavior:roundUp];
NSLog(@"Rounded total: %@", total);
//比较大小
NSDecimalNumber*discount1 = [NSDecimalNumberdecimalNumberWithString:@".85"];
NSDecimalNumber*discount2 = [NSDecimalNumberdecimalNumberWithString:@".9"];
NSComparisonResult result = [discount1 compare:discount2];
if (result ==NSOrderedAscending) {
NSLog(@"85%% < 90%%小于");
} else if (result == NSOrderedSame) {
NSLog(@"85%% == 90%%等于");
} elseif (result ==NSOrderedDescending) {
NSLog(@"85%% > 90%%大于");
}
-(NSString *)notRounding:(NSString*)price afterPoint:(NSInteger)position
{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundDown
scale:position raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
// ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
ouncesDecimal = [[NSDecimalNumber
alloc]initWithString:price];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
// return roundedOunces;// 整数的不带小数点
NSString* string = [NSString
stringWithFormat:@"%@",roundedOunces];
if ([string
rangeOfString:@"."].length==0) {
string= [string stringByAppendingString:@".00"];
}else{
NSRange range = [string
rangeOfString:@"."];
if (string.length-range.location-1==2)
{
}else{
string= [string stringByAppendingString:@"0"];
}
}
return string;//整数.00格式
}
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundBankers
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
枚举
NSRoundPlain, // Round up on a tie //貌似取整
NSRoundDown, // Always down == truncate //只舍不入
NSRoundUp, // Always up // 只入不舍
NSRoundBankers // on a tie round so last digit is even 貌似四舍五入
//90.7049+0.22 然后四舍五入
NSDecimalNumber *subtotal = [NSDecimalNumberdecimalNumberWithString:@"90.7049"];
NSDecimalNumber *discount = [NSDecimalNumberdecimalNumberWithString:@"0.22"];
NSDecimalNumber *total = [subtotal decimalNumberByAdding:discount withBehavior:roundUp];
NSLog(@"Rounded total: %@", total);
NSDecimalNumber*price1 = [NSDecimalNumberdecimalNumberWithString:@"15.99"];
NSDecimalNumber*price2 = [NSDecimalNumberdecimalNumberWithString:@"29.99"];
NSDecimalNumber*coupon = [NSDecimalNumberdecimalNumberWithString:@"5.00"];
NSDecimalNumber*discount = [NSDecimalNumberdecimalNumberWithString:@".90"];
NSDecimalNumber*numProducts = [NSDecimalNumberdecimalNumberWithString:@"2.0"];
NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];
NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon];
NSDecimalNumber *afterDiscount = [afterCoupon decimalNumberByMultiplyingBy:discount];
NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
NSDecimalNumber*averageSquared = [average decimalNumberByRaisingToPower:2];
的NSLog(@“小计:%@”,加);/ / 45.98
的NSLog(@“优惠券后:%@”,减);/ / 40.98
的NSLog((@“折后:%@”),乘);/ / 36.882
的NSLog(@“每股平均价产品:%@”,平均);/ / 18.441
的NSLog(@“平均价的平方:%@”,averageSquared);/ / 340.070481
//保留小数点后两位
NSDecimalNumberHandler*roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundUp
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
//9折后 36.882 有余进位结果 36.89
NSDecimalNumber*subtotal = [NSDecimalNumberdecimalNumberWithString:@"40.98"];
NSDecimalNumber*discount = [NSDecimalNumberdecimalNumberWithString:@".90"];
NSDecimalNumber*total = [subtotal decimalNumberByMultiplyingBy:discount
withBehavior:roundUp];
NSLog(@"Rounded total: %@", total);
//比较大小
NSDecimalNumber*discount1 = [NSDecimalNumberdecimalNumberWithString:@".85"];
NSDecimalNumber*discount2 = [NSDecimalNumberdecimalNumberWithString:@".9"];
NSComparisonResult result = [discount1 compare:discount2];
if (result ==NSOrderedAscending) {
NSLog(@"85%% < 90%%小于");
} else if (result == NSOrderedSame) {
NSLog(@"85%% == 90%%等于");
} elseif (result ==NSOrderedDescending) {
NSLog(@"85%% > 90%%大于");
}
-(NSString *)notRounding:(NSString*)price afterPoint:(NSInteger)position
{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundDown
scale:position raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
// ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
ouncesDecimal = [[NSDecimalNumber
alloc]initWithString:price];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
// return roundedOunces;// 整数的不带小数点
NSString* string = [NSString
stringWithFormat:@"%@",roundedOunces];
if ([string
rangeOfString:@"."].length==0) {
string= [string stringByAppendingString:@".00"];
}else{
NSRange range = [string
rangeOfString:@"."];
if (string.length-range.location-1==2)
{
}else{
string= [string stringByAppendingString:@"0"];
}
}
return string;//整数.00格式
}
相关文章推荐
- ios属性和成员变量写在.h文件和.m文件中 区别?
- iOS 多层依赖关系的代码联调
- 提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果
- IOS 高效利用Xcode
- iOS开发零基础教程之Git的一些常用命令
- iOS开发之autoreSizing
- iOS开发:记录开发中遇到的编译或运行异常以及解决方案
- Firemonkey使用iOS的第三方静态库(Link Binary With Libraries)
- 正则表达式在iOS中的运用
- iOS开发debug集锦
- iOS开发 Block的用法
- iOS 8 AutoLayout与Size Class
- iOS 9 Auto Layout界面自动布局系列6-自适应布局
- iOS 8 Auto Layout界面自动布局系列5-自身内容尺寸约束、修改约束、布局动画
- IOS消息推送之APNS
- iOS开发多线程篇—多线程简单介绍
- iOS 的播放影片
- iOS代理模式设置方法
- iOS笔记--oc语法8 (@synthesize和@dynamic的区别)
- iOS常用的第三方库