5

I have an input with UIKeyboardTypeDecimalPad and I need my user to input a float (with unlimited characters after a dot). After the input I filter the string with :

NSString *newValue = [NSString stringWithFormat:@"%.f",[textField.text floatValue]]

But that gives me a lot of unnecessary digits after a dot (for example for 2.25 it gives 2.249999).

All I need is to filter the input so it'll be a legal float (digits and not more than one dot).

How do I do that?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Valentin Stoianoff
  • 187
  • 3
  • 3
  • 11

3 Answers3

12
NSString *newValue = [NSString stringWithFormat:@"%0.1f", [textField.text floatValue]];

the number after the dot is the number of decimal places you want.

UPDATE: You could use string manipulation to determine the number of decimal places the user typed in (don't forget to check for edge cases):

NSInteger numberOfDecimalPlaces = textString.length - [textString rangeOfString:@"."].location - 1;

and then if you want to create a new string with a new float to the same level of display precision you could use:

NSString *stringFormat = [NSString stringWithFormat:@"%%0.%if", numberOfDecimalPlaces];
NSString *newString = [NSString stringWithFormat:stringFormat, newFloat];
Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81
  • I know that, but there is no number of decimals I want, it could be 1-10 numbers after a dot and I don't want it to look like 2.2500000000. – Valentin Stoianoff Feb 03 '12 at 07:59
  • 1
    I'm sorry but your question wasn't clear. Are you saying you want the number of decimals to match the number of decimals the user typed in? – Magic Bullet Dave Feb 03 '12 at 08:09
  • 1
    Bear in mind that when parsing user inputted numerical values from strings they do not always use the decimal point. In many locales (eg France, Germany) they are exchanged in meaning with a comma. You're probably better off using either an NSNumberFormatter or NSScanner to parse the localised string into a number. – Samscam Apr 17 '13 at 14:42
  • Could you please explain this - `%%0.%if`? Thanks. – Unheilig May 23 '15 at 04:41
  • 1
    @Unheilig, the first line creates a string that can be used as a number format on the second line. For example if the numberOfDecimalPlaces is 2, then the string becomes: @"%0.2f". Double % just escapes the first % and the %i is substituted with the int. Dave. – Magic Bullet Dave Jun 01 '15 at 14:48
  • @MagicBulletDave Thanks so much. – Unheilig Jun 11 '15 at 04:49
2

Not sure if this is what you want but try something like the following:

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
// set to long number of decimals to accommodate whatever a user might enter
[nf setMaximumFractionDigits:20]; 
NSString *s = [nf stringFromNumber:
               [NSNumber numberWithDouble:[userEnteredNumberString doubleValue]]
               ];
NSLog(@"final:%@",s);
ragamufin
  • 4,113
  • 30
  • 32
-1

Try using a double instead of float. I think the double removes all trailing zero's.

Darren
  • 10,182
  • 20
  • 95
  • 162