Decimal point in as calculations. or,
If I use a decimal keyboard to enter numbers, the decimal changes depend on the country and region format.
Maybe as a period ". Or as a comma", "
And I have no control on which device the application is used on.
If the region format uses a comma, the calculation becomes incorrect. Entering 5.6 is the same as entering only 5 times. and 56 times.
And that's even if I programmatically allow both. and as input to the TextField.
How can I get around this without using the numbers in the punctuation bar and probably should also give instructions to avoid the semicolon input ","
This only inputting the numbers and decimal numbers I need and the decimal pane is much nicer.
One way is to check if the textField contains ",".
If it contains, replace it with "." and perform all arithmetic operations.
As with any case, you will read all text fields and text elements as an NSString object, you can manipulate the input value and transform it according to your needs.
Also showing the result, replace "." with "," to make the user feel comfortable according to the regional formats.
You are using NSNumberFormatter for this , as this can be configured to handle different locales.
Create formatter:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[NSLocale currentLocale]];
Use it:
NSNumber *number = [numberFormatter numberFromString: string]; //string is the textfield.text
if device locales are set to locale where decimal separator is in ,
, will use the numeric keypad and formatter as. On them, the grouping separator will be.
For other locales, this will be the opposite.
NSNumberFormatter is very complex, you should read its section in the Data Formatter Guide . It also knows a lot of currency handling (display, not conversion), if your application actually handles such.
You can also use NSNumberFormatter class method
NSString* formattedString = [NSNumberFormatter
localizedStringFromNumber:number
numberStyle:NSNumberFormatterCurrencyStyle];
Identify - the local country uses a comma for the decimal point
var isUsesCommaForDecimal : Bool {
let nf = NumberFormatter()
nf.locale = Locale.current
let numberLocalized = nf.number(from: "23,34")
if numberLocalized != nil {
return true
} else {
return false
}
}