gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:30:01 25 4
gpt4 key购买 nike

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

我的代码是

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;

最佳答案

float a=2.245;
float b=ceilf(a*100)/100;

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[currencyFormatter setRoundingMode:NSNumberFormatterRoundCeiling];
[currencyFormatter setMaximumFractionDigits:2];

关于iphone - 十进制值四舍五入到 ios 中的下一个更高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17986616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com