If I use decimal pad for input of numbers the decimal changes depending of country and region format.
May be as a point "." or as a comma ","
And I do not have control over at which device the app is used.
If the region format uses a comma the calculation gets wrong. Putting in 5,6 is the the same as putting in only 5 some times and as 56 same times.
And that is even if I programmatically allow both . and , as input in a TextField.
How do I come around this without using the numbers an punctation pad and probably also have to give instructions to avoid input with comma ","
It is only input for numbers and decimal I need and the decimal pad is so much nicer.

- 52,040
- 14
- 137
- 178

- 501
- 1
- 10
- 27
-
plz accept Viki's answer. My answer is not good, and I'd like to remove my answer. – Anoop Vaidya May 12 '15 at 06:11
4 Answers
You shoudld use a NSNumberFormatter for this, as this can be set to handle different locales.
Create a formatter:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[NSLocale currentLocale]];
Use it:
NSNumber *number = [numberFormatter numberFromString: string]; //string is the textfield.text
if the device's locale is set to a locale, where the decimal separator in a ,
, the Number Keypad will use is and the formatter as-well. On those the grouping separator will be .
For the other locales it will be vice-versa.
NSNumberFormatter is very sophisticated, you should read its section in Data Formatter Guide, too. It also knows a lot of currency handling (displaying, not conversion), if your app does handle such.

- 34,483
- 6
- 89
- 91

- 52,040
- 14
- 137
- 178
-
Thank you. Very good and elegant. I have places where this come in handy. At the app I was going to use this now I think the minus sign is so useful so it as good to use the numbers and punctation pad after all – Lars - Mar 04 '13 at 11:28
You can use also the class method of NSNumberFormatter
NSString* formattedString = [NSNumberFormatter
localizedStringFromNumber:number
numberStyle:NSNumberFormatterCurrencyStyle];

- 7,647
- 3
- 41
- 71
Identify is local country uses comma for 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
}
}

- 1,391
- 12
- 21
One way could be to check if the textField contains a ",".
If it contains, replace it with "." and do all arithmetic operations.
As anyways you will be reading all textFields and textViews as NSString object, you can manipulate the input value and transform it according to your need.
Also while showing the result replace "." with "," so that user feel comfortable according to there regional formats.

- 46,283
- 15
- 111
- 140
-
6
-
OMG... you are just awesome. Your range of thinking is tooo vast then me. I didn't think about that. :( – Anoop Vaidya Mar 04 '13 at 10:54
-
-
@vikingosegundo: So you are the best person to answer this question :) – Anoop Vaidya Mar 04 '13 at 10:56
-
1
-
-
1@Lars: Even I agree to viki's comment as this can be correct only for a limited patterns of number. – Anoop Vaidya Mar 04 '13 at 11:11
-
Thanks, yes I will probably have to do that check. What vikingosegundo answered I don´t know how to. Is there a way to control 10,000.00 or 10.000,00 programmatically for the keypad? numberFormater? – Lars - Mar 04 '13 at 11:11
-
If your number is not coming or going to any server or database, You can mention in your app or as a tooltip "only US format of 100000.00 allowed) – Anoop Vaidya Mar 04 '13 at 11:13
-
Yes it is only local in the app. But I can´t recommend the users to set in a other region format to use the app, then it is better to use numbers and punctation pad even if it is ugly. But also I found out I could use the minus sign there and it is one way to delete an input number because a button to delete is a little dangerous in my app because it is easy to accidentally touch that button and the user has lost what he wanted to have. Better to do that with minus in the textField – Lars - Mar 04 '13 at 11:19
-
You can surely go for numberformatter, as I mentioned in my comment, and the implementation is shown by viki in his answer. – Anoop Vaidya Mar 04 '13 at 11:21