0

I am building a calculator and have run into a problem with displaying unnecessary 0's (it isn't displaying them and I want it to be).

An example of what it does now is as follows. [Represents Calculator Screen]

[Blank Calculator Screen]

User hits "4"

[4]

User hits "."

[4]

User hits "0"

[4]

User hits "0"

[4]

User hits "5"

[4.005]

Instead, I want to make it do the following:

[Blank Calculator Screen]

User hits "4"

[4]

User hits "."

[4.]

User hits "0"

[4.0]

User hits "0"

[4.00]

User hits "5"

[4.005]

I use a number formatter as shown below...

 formatter = [[NSNumberFormatter alloc] init];
     formatter.numberStyle = NSNumberFormatterDecimalStyle;
     formatter.maximumIntegerDigits = 13;
     formatter.minimumFractionDigits = 0;
     formatter.maximumFractionDigits = 13;
     formatter.maximumSignificantDigits = 13;
     formatter.usesGroupingSeparator = YES;
     formatter.groupingSeparator = @",";
     formatter.decimalSeparator = @".";
     formatter.generatesDecimalNumbers = true;

I use the following to call my numbers...

 buttonScreen.text = [formatter stringFromNumber:
 [NSNumber numberWithDouble:currentNumber]]

Thanks!

4

2 回答 2

1

它没有给您零,因为 4 是 4、4.0、4.00 等的精确浮点表示。我建议您将用户输入存储到 a中,然后在需要进行实际数学运算时NSString *将其转换为 a 。NSNumber

于 2013-02-22T21:13:49.033 回答
0

您可以通过在“=”或按下操作键之前不评估输入来解决此问题。

一旦输入完成,您只需添加到字符串并将字符串转换为正确的数字,您知道这是因为按下了不同的键。

于 2013-02-22T21:15:16.317 回答