【问题标题】:No zero before the decimal point or after second decimal小数点前或小数点后没有零
【发布时间】:2012-07-02 19:10:49
【问题描述】:

我的应用中有简单的计算

-(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

我的问题是小数点前或小数点后没有零

【问题讨论】:

标签: ios xcode zero decimal-point


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 2016-12-14
    • 2011-11-14
    • 1970-01-01
    相关资源
    最近更新 更多