十进制值四舍五入到ios中的下一个更高的值

编程入门 行业动态 更新时间:2024-10-17 13:28:43
本文介绍了十进制值四舍五入到ios中的下一个更高的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用程序中,我接受金额值(浮动)并保存它。在金额字段中,用户可以在小数点后输入任意数量的位数。但是当我读它并保存时,它应该只是小数点后的两位数。此方案适用于我的代码。但问题是,如果用户输入值2.468,则保存为2.47。但如果用户输入是2.245,在这种情况下我需要保存为2.25但它只保存为2.24。

In my app, am accepting amount value(float) and saving it. In the amount field, the user may enter any number of digits after decimal point. But when i read it and save, it should be only two digits after the decimal point. this scenario works properly with my code. but the problem is, if the user enters the value 2.468, it is saved as 2.47. but if the user input is 2.245, in this case i need to be saved as 2.25 but it gets saved as 2.24 only.

我的代码是

AmountValue = [NSString stringWithFormat:@"%f",[AmountTxt1.text floatValue]]; float Rate_int = [AmountValue floatValue]; NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; NSString *str1 = [NSString stringWithFormat:@"%@",[currencyFormatter stringFromNumber:[NSNumber numberWithFloat:Rate_int]]]; TotalAmtTxt.text = str1;

推荐答案

double Rate_int1 = [input doubleValue]; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setRoundingMode: NSNumberFormatterRoundHalfUp]; [formatter setMaximumFractionDigits:2]; NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithDouble:Rate_int1]];

NSString numberString具有舍入值..

The NSString numberString has the rounded off value..

更多推荐

十进制值四舍五入到ios中的下一个更高的值

本文发布于:2023-11-10 02:45:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更高   四舍五入   十进制值   ios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!