Nsnumberformatter with Comma Decimal Separator

NSNumberFormatter with comma decimal separator

So the question is valid : I'm answering it myself :)

I have a decimal value read from sqlite (e.g 12000 or 12000.54) directly transformed into NSString.
I have to use different separator at some point in my code.

NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@""];
[formatter setDecimalSeparator:@"."];

// Decimal values read from any db are always written with no grouping separator and a comma for decimal.

NSNumber *numberFromString = [formatter numberFromString:@"12000.54"]];

[formatter setGroupingSeparator:@" "]; // Whatever you want here
[formatter setDecimalSeparator:@","]; // Whatever you want here

NSString *finalValue = [formatter stringFromNumber:numberFromString];

NSLog(@"%@",finalValue); // Print 12 000,54

Problem solved.

How to convert float to NSString with thousand separator and 2 decimal points?

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencySymbol:@""]; //To remove currency symbol
NSLog(@"%@", [currencyFormatter stringFromNumber:[NSNumber numberWithDouble:[_goodPrice floatValue]]]);

NSNumberFormatter not adding comma after decimal point

Grouping separator is used to separate digits in the integer part of a number and is not used in the fractional part. There's no built-in way to achieve this effect. If you only support decimal numbers, you could manually parse the formatted string and insert a comma after each 3 characters, starting with the decimal separator.

NSMutableString *s = [NSMutableString stringWithString: formattedString];
NSUInteger decimalSeparatorLocation = [s rangeOfString: @"."].location;
if (decimalSeparatorLocation != NSNotFound) {
for (NSUInteger i = decimalSeparatorLocation + 4; i + 1 < [s length]; i += 4)
[s insertString: @"," atIndex: i];
}

Warning: this code is typed on an iPad (c).

NSNumberFormatter with number style NSNumberFormatterCurrencyStyle trouble with commas

Each Locale determines if it shows cents or just Krona. So, if you want to force it to show digits after the comma, add the line:

[numberFormatter setMinimumFractionDigits:2];

to get two digits after comma.

But the example you show has correct output.

NSNumberFormatter change decimal separator style

There is a little thing called Localization. Every language uses a different character as a decimal separator. Where English uses a decimal point, other languages use a comma (or other characters).

When you create a NSNumberFormatter it uses the system locale (NSLocale instance) to decide about the decimal separator (and grouping separator and other things).
If you want a fixed behavior, then just set a different locale using [NSNumberFormatter setLocale:]

Also note there is one special kind of locale
[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] which defines a special locale which cannot be changed by user settings and is always the same.

In this case, using a NSNumberFormatter is not the best idea. This is one of the cases when you want to use a regular expression.



Related Topics



Leave a reply



Submit