Reputation: 22708
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?
Upvotes: 12
Views: 9093
Reputation: 832
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.
Upvotes: 6
Reputation: 1481
The best way is get the decimal separator from the current locale without init any NSNumberFormatter
object.
Like this:
[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]
Upvotes: 21
Reputation: 22701
decimalSeparator
Returns a string containing the character the receiver uses to represent decimal separators.
- (NSString *)decimalSeparator
Example:
decimalSymbol = [f decimalSeparator];
Upvotes: 2
Reputation: 90117
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
NSString *decimalSymbol = [formatter decimalSeparator];
Upvotes: 30