Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the decimal symbol?

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?

like image 339
CristiC Avatar asked Mar 22 '11 20:03

CristiC


1 Answers

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

like image 106
Mikolaj Avatar answered Sep 19 '22 06:09

Mikolaj