12

I want to get which is the decimal symbol set up on the device. Until now I was using 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 was working fine until now when I am testing on another device (4.3) - null is returned. What could be the problem?

Is there another way to retrive the decimal symbol?

LATER EDIT:
I can use:

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

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

5 Answers 5

30
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
NSString *decimalSymbol = [formatter decimalSeparator];
21

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

[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]
1
  • 2
    Or from iOS 10+, [NSLocale currentLocale].decimalSeparator;
    – CyberMew
    Feb 7, 2017 at 7:37
13

Swift 4 version:

Locale.current.decimalSeparator
6

Swift 4

You can use Locale.current.decimalSeparator or NSLocale.current.decimalSeparator.

The Swift overlay to the Foundation framework provides the Locale structure, which bridges to the NSLocale class.

and

use NSLocale when you need reference semantics or other Foundation-specific behavior.

NSLocale Apple doc

2

decimalSeparator

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

- (NSString *)decimalSeparator

Example:

decimalSymbol = [f decimalSeparator];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.