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
Ios13 Share Sheet: How to Set Preview Thumbnail When Sharing Uiimage
How to Stop Symbolicate Adding "<Redacted>" Pieces to iOS Crash Log
Setting Up a Parent-Child Relationship in Core Data
Region Monitoring Glitch on iOS 7 - Multiple Notifications at the Same Time
Nsgenericexception Reason Collection <Nsconcretemaptable: Xxx>
How to Access an Xcassets Directory on the Filesystem
After Updating to Xcode 10.2: Invalid Toolchain Error When Trying to Submit App to App Store
What's the Point of Nsassert, Actually
Proper Usage of Intrinsiccontentsize and Sizethatfits: on Uiview Subclass with Autolayout
How to Position Views on Top of Each Other
Get Country Code from Country Name in iOS
iOS Animate/Morph Shape from Circle to Square
Simple Low-Latency Audio Playback in iOS Swift
Is It Ok to Place Most Logic and Models in the Appdelegate
How to Share Nsdata or Phasset Video Using Facebook iOS Sdk 4.0 Fbsdksharedialog
Uiscrollview Pauses Nstimer While Scrolling
Arc4Random_Uniform Not Available in Xcode 7.0 Beta (7A176X) on Osx 10.10.4