1

I have a string that represents a float, for example 2400.0. I want to format it as digit (2,400.0) and I need to keep the zero after the digit symbol.

NSString* theString = @"2400.0"; 

// I convert the string to a float 
float f = [theString floatValue]; 
// here I lose the digit information :( and it ends up with 2400 instead of 2400.0

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setUsesSignificantDigits:YES];
[formatter setMinimumFractionDigits:1];
[formatter setMaximumFractionDigits:2];
[formatter setLocale:[NSLocale currentLocale]];

NSString *result = [formatter stringFromNumber:@(f)];

The NSLog of result is 2,400 while I need 2,400.0

How can I obtain the right string?

MatterGoal
  • 16,038
  • 19
  • 109
  • 186

4 Answers4

4

You probably want to set the minimumFractionDigits to 1 (and your maximumFractionDigits as well in this case).

You also probably don't want to use significant digits. The following code yields the desired output:

NSString *theString = @"2400.0";

float f = [theString floatValue];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:1];
[formatter setMaximumFractionDigits:1];
[formatter setLocale:[NSLocale currentLocale]];

NSLog(@"%@", [formatter stringFromNumber:@(f)]);
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • the problem is that during the first conversion from string to float I lose the ".0" information. I've already tried to set `minimumFractionDigits`. Btw I add it to the my code example to be more precise. – MatterGoal Jan 31 '14 at 08:59
  • I don't understand how you can lose the `.0` (a different fraction I can understand, but `.0`?) Anyway, you'll need two number formatters: one for input and another one for output. – DarkDust Jan 31 '14 at 09:06
  • Oh, and since you set `setUsesSignificantDigits:YES` you might want to set the [`minimumSignificantDigits`](https://developer.apple.com/library/mac/documentation/cocoa/reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNumberFormatter/setMinimumSignificantDigits:) as well (see [this question](http://stackoverflow.com/questions/1322348/what-describes-nsnumberformatter-maximumsignificantdigits)) or turn them off. – DarkDust Jan 31 '14 at 09:09
  • What do you mean with 2 number formatters? how I use the Input one? – MatterGoal Jan 31 '14 at 09:29
  • If you try to take the code that I wrote... you get exactly the string `2,400` but I want to obtain `2,400.0` – MatterGoal Jan 31 '14 at 09:32
  • I thought you're also parsing the string with the number formatter, missed the `[theString floatValue]` part. I've edited my answer with sample code that yields your desired output. – DarkDust Jan 31 '14 at 09:47
0

Te below solves the issue

NSString *formatString = @"0,000.0";
[formatter setPositiveFormat:formatString];

The number of 0's after decimal will be used for formatting. It works for negative numbers also.

you should dynamically change the format string based on number of digits of the float value before and after decimal point.

Hope it helps.

Sathe_Nagaraja
  • 163
  • 1
  • 9
  • 1
    I need to keep the locale information. This is not a solution. Many countries use a different format like `@"0.000,0"` – MatterGoal Jan 31 '14 at 09:45
  • Agree, it is not a generic solution. However it serves the question asked "need a format for 0,000.0" for generic solution we need to set locale to formatter. – Sathe_Nagaraja Jan 25 '18 at 00:29
0

easy,

then you go to display it just do this:

myLabel.text = @"%0.1f", f;
MoralCode
  • 1,954
  • 1
  • 20
  • 42
0

Use minimumFractionDigits in addition to maximumFractionDigits:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.minimumFractionDigits = 1;
formatter.maximumFractionDigits = 1;

XCTAssert([[formatter stringFromNumber:@(2400.0)] isEqualToString:@"2400.0"]);
XCTAssert([[formatter stringFromNumber:@(2400.1)] isEqualToString:@"2400.1"]);
pkamb
  • 33,281
  • 23
  • 160
  • 191