How to get a decimal character?

I want to get which is the decimal character installed on the device. So far I have used this method:

NSString *decimalSymbol; NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; [f setMinimumFractionDigits:2]; [f setMaximumFractionDigits:2]; [f setGroupingSeparator:@" "]; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setMinimumFractionDigits:2]; [formatter setMaximumFractionDigits:2]; [formatter setGroupingSeparator:@" "]; NSRange range = {1,1}; decimalSymbol = [[formatter stringFromNumber:[f numberFromString:[@"" stringByAppendingFormat:@"%.02f", 1.0f]]] substringWithRange:range]; [formatter release]; [f release]; 

It worked fine so far, when I test another device (4.3) - returns null . What could be the problem?

Is there any other way to get the decimal character?

MORE CHANGE:
I can use:

 decimalSymbol = [[@"" stringByAppendingFormat:@"%.02f", 1.0f] substringWithRange:range]; 

but why the other way it does not work on this particular device?

+6
objective-c iphone cocoa-touch nsnumberformatter
source share
4 answers
 NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; NSString *decimalSymbol = [formatter decimalSeparator]; 
+24
source share

The best way is to get the decimal separator from the current locale without init any NSNumberFormatter object. Like this:

 [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator] 
+20
source share

DecimalSeparator

Returns a string containing the character that the receiver uses to represent decimal separators.

- (NSString *)decimalSeparator

Example:

decimalSymbol = [f decimalSeparator];

+2
source share

Swift 3 version:

NSLocale.current.decimalSeparator

+1
source share

All Articles