今天遇到一个问题 服务器返回货币数据 妈的 用string 》 floatvalue 不准确
去百度查查 妈的国人分享精神真差 真他妈的自私 一个破壁文章没几个字 还是从国外翻译过来的 全他妈转发 很不详细
弄明白就不能 再出一个完整点的 都是从菜鸟过来的 尼玛
今天去国外查完 分享下
//貌似 格式化的帮手 妈的 我英语高考61分 我咋知道它叫啥
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);
//另一个方法 不准 推荐不用 国内网站一个煞笔 告诉我们的
NSLog(@"%f", round([[NSStringstringWithFormat:@"%f",12345.6749] floatValue]*100)/100);
NSDecimalNumber 其他用法
所有NSDecimalNumber对象是不可变的,这意味着已经被创建后不能改变它们的值。
NSDecimalNumber*price;
price = [NSDecimalNumberdecimalNumberWithMantissa:1599
exponent:-2 //10的-2次方
isNegative:NO];
等效
price = [NSDecimalNumberdecimalNumberWithString:@"15.99"];
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((@“折后:%@”),9/10);/ / 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%%大于");
}