Decimal localization in UITextField

Hi everyone!


I am having difficulty finding the answer to an issue with localizing a medical calculaor in countiries that use a comma for a decimal separator. When I run the app with the location set for Italy, for example, the comma shows on the decimal pad keyboard, but the numbers entered into the text field do not show the comma when the button is tapped. The enterd vaue doesn't show the separator either in a print() statement. The period shows when the locale is set for US.


I haven't found any goo tutorial or examples ifor NumberFormatter(). Any suggestions?

Accepted Reply

Dirk,

Thanks for the prompt reply!


Yes, we are using the Decimal Pad Keyboard. The comma show on the keboard, but not in the text field. The calculation also do not recognize the decimal. The calculator woks fine in a locale that uses a period as a decimal seperator.


I just checked for other delegate methods, and it looks like a colleague had hardcoded a test for a leading period, but not a decimal. The comma, now shows in the UITextField! Now I just need to use number formatter, so the method will acept the data.


Thanks for helping to point me in the right direction.


Eric

Replies

What kind of UITextField do you use? Or, to be more specific, what kind of keyboardType?.

If I use UIKeyboardTypeDecimalPad, a comma appears as soon as I tap on the corresponding button. No special setup needed.

The UITextField does not inputcheck on it's own. So I can enter multiple commas without any problem, although not being a syntactical correct number.


Do you have any delegate method that may interfere?


Dirk

When I run the app with the location set for Italy …

In addition to what just.do.it said…

Be aware that the language in which your app is running is independent (the localisation) of the region (the locale). You set the former with Settings > General > Langauge & Region > iPhone Language and the latter with Settings > General > Langauge & Region > Region. Or, during debugging, the Application Language and Application Region popups in the Options tab in the scheme editor. Most folks have these in sync but it’s possible to set them independently (because that makes sense in certain user scenarios) and it’s easy for you code to make mistakes that only show up in that case.

The enterd vaue doesn't show the separator either in a print() statement.

Be warning that

print
is not locale aware. If you want to render a number in a locale-sensitive fashion, use
NumberFormatter
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 5 Jun through to Fri, 9 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

Dirk,

Thanks for the prompt reply!


Yes, we are using the Decimal Pad Keyboard. The comma show on the keboard, but not in the text field. The calculation also do not recognize the decimal. The calculator woks fine in a locale that uses a period as a decimal seperator.


I just checked for other delegate methods, and it looks like a colleague had hardcoded a test for a leading period, but not a decimal. The comma, now shows in the UITextField! Now I just need to use number formatter, so the method will acept the data.


Thanks for helping to point me in the right direction.


Eric

Quinn,


Thanks for your prompt response as well!


I resolved the issue with showing the comma in the UITextField, as above.


Now I just need to apply the NumberFormatter, I guess in the delgate.


Thanks for your help,


Eric