views:

44

answers:

2

For example, I have float values like:

0.2 1.5 2.0 3.0 10.0 52.5 60.0

Under any circumstances, I really want one fractional digit. Even if the number is exactly 1, i want it to be displayed as 1.0

I tried to set up an NSNumberFormatter like this, but it doesn't work:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setAllowsFloats:YES];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setMinimumFractionDigits:1];

[formatter setUsesSignificantDigits:YES];
[formatter setMinimumSignificantDigits:2];

[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];

It shows 0.2, 5.0, 5.5, 9.0,

but when it reaches 10, it would show 10, 10.3, 11, 11.9, 15, ... and so on. Is this something NSNumberFormatter can't do?

+2  A: 

try this?

NSString * f= [NSString stringWithFormat:@"%.01f",(float)10];

madmik3
This works fine! Thanks! But it wouldn't localize the comma...
openfrog
Not sure if I understand, but if you are in a place where the comma us used for the . it should work just fine. http://www.cplusplus.com/reference/clibrary/clocale/lconv/
madmik3
+1  A: 

setAlwaysShowsDecimalSeparator: Controls whether the receiver always shows the decimal separator, even for integer numbers.

Try setting this to YES

Bahadır
It will force a decimal separator, but it doesn't make sure there's always exactly one fractional digit after the separator... it produces things like 10. or 15. but not 10.0 or 15.5 ...
openfrog