今天遇到一個問題 服務器返回貨幣數據 媽的 用string 》 floatvalue 不許確git
去百度查查 媽的國人分享精神真差 真他媽的自私 一個破壁文章沒幾個字 仍是從國外翻譯過來的 全他媽轉發 很不詳細服務器
弄明白就不能 再出一個完整點的 都是從菜鳥過來的 尼瑪ide
今天去國外查完 分享下 網站
//貌似 格式化的幫手 媽的 我英語高考61分 我咋知道它叫啥spa
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler翻譯
decimalNumberHandlerWithRoundingMode:NSRoundBankersorm
scale:2對象
raiseOnExactness:NOip
raiseOnOverflow:NOci
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%%大於");
}