小数点前或零小数后没有零

时间:2012-07-02 19:10:50

标签: ios xcode zero decimal-point

我在我的应用程序中进行了简单的计算

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

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

1 个答案:

答案 0 :(得分:4)

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

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

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

相关问题