It's not giving you the zeroes because 4 is the exact floating point representation of 4, 4.0, 4.00, etc. I suggest that you store the user input into a NSString *
, which you then convert into an NSNumber
when you need to do actual math.
Show Unnecessary "0" inputs
-
11-03-2022 - |
سؤال
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!
المحلول
نصائح أخرى
You could get around this issue by not evaluating the input before "=" or an operation key is pressed.
You would just add to the string and transform the string into the correct number once the entry is finished which you know because a different key has been pressed.