gpt4 book ai didi

ios - 小数点前或小数点后无零

转载 作者:行者123 更新时间:2023-12-01 18:28:10 28 4
gpt4 key购买 nike

我的应用程序中有简单的计算

-(IBAction)calculate {
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setRoundingMode: NSNumberFormatterRoundHalfUp];
[nf setMaximumFractionDigits: 2];

float value1 = [[nf numberFromString: textField1.text] floatValue];
float value2 = [[nf numberFromString: textField2.text] floatValue];

float x = value1 * value2 / 100;
label2.text = [nf stringFromNumber: [NSNumber numberWithFloat: x]];

[nf release];
}

现在,当我计算结果时,如下所示: ,7564,5
我需要我的结果看起来像这样: 0,7564,50
我的问题是小数点之前或之后的第二个小数位没有零

最佳答案

在UILabel中设置文本的位置,使用

label2.text = [NSString stringWithFormat:@"%.2f",x];

这将确保您获得2个小数位。您甚至不需要数字格式化程序。

关于ios - 小数点前或小数点后无零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299748/

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