iOS|iOS NSDecimalNumber的一些简单理解和使用

遇到一个问题 服务器返回货币数据 妈的 用string 》 floatvalue 不准确

NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandlerdecimalNumberHandlerWithRoundingMode:NSRoundBankersscale:2raiseOnExactness:NOraiseOnOverflow:NOraiseOnUnderflow:NOraiseOnDivideByZero: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 = [NSDecimalNumber decimalNumberWithString:@"90.7049"]; NSDecimalNumber *discount = [NSDecimalNumber decimalNumberWithString:@"0.22"]; NSDecimalNumber *total = [subtotal decimalNumberByAdding:discount withBehavior:roundUp]; NSLog(@"Rounded total: %@", total); NSDecimalNumber 其他用法所有NSDecimalNumber对象是不可变的,这意味着已经被创建后不能改变它们的值。NSDecimalNumber*price; price = [NSDecimalNumber decimalNumberWithMantissa:1599exponent:-2//10的-2次方isNegative:NO]; 等效price = [NSDecimalNumber decimalNumberWithString:@"15.99"]; NSDecimalNumber*price1 = [NSDecimalNumber decimalNumberWithString:@"15.99"]; NSDecimalNumber*price2 = [NSDecimalNumber decimalNumberWithString:@"29.99"]; NSDecimalNumber*coupon = [NSDecimalNumber decimalNumberWithString:@"5.00"]; NSDecimalNumber*discount = [NSDecimalNumber decimalNumberWithString:@".90"]; NSDecimalNumber*numProducts = [NSDecimalNumber decimalNumberWithString:@"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]; subtotal的NSLog(@“小计:%@”,price1加price2后); / / 45.98 afterCoupon的NSLog(@“subtotal优惠券后:%@”,subtotal减coupon); / / 40.98 afterDiscount的NSLog((@“折后:%@”),9/10); / / 36.882 average的NSLog(@“每股平均价产品:%@”,平均); / / 18.441 averageSquared的NSLog(@“平均价的平方:%@”,averageSquared); / / 340.070481//保留小数点后两位NSDecimalNumberHandler*roundUp = [NSDecimalNumberHandlerdecimalNumberHandlerWithRoundingMode:NSRoundUpscale:2raiseOnExactness:NOraiseOnOverflow:NOraiseOnUnderflow:NOraiseOnDivideByZero:YES]; //9折后 36.882 有余进位结果 36.89NSDecimalNumber*subtotal = [NSDecimalNumber decimalNumberWithString:@"40.98"]; NSDecimalNumber*discount = [NSDecimalNumber decimalNumberWithString:@".90"]; NSDecimalNumber*total = [subtotal decimalNumberByMultiplyingBy:discountwithBehavior:roundUp]; NSLog(@"Rounded total: %@", total); //比较大小NSDecimalNumber*discount1 = [NSDecimalNumber decimalNumberWithString:@".85"]; NSDecimalNumber*discount2 = [NSDecimalNumber decimalNumberWithString:@".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%%大于"); }

【iOS|iOS NSDecimalNumber的一些简单理解和使用】以上只是简单的使用介绍不足之处请大家指正,共同学习进步,谢谢。

    推荐阅读