UKDataGeek
UKDataGeek

Reputation: 6912

Localisation issue? Number being trimmed at comma when extracting from Textfield

I have a Mac app that is trying to get data from a text field.
I populate the text field by taking the 3001 using:

mytextfield.integerValue = 3001

It then displays the value in the text field of 3,001. when I extract it using

let numberResult = mytextfield.integerValue  //or mytextfield.doublevalue

it returns the number 3 ( ie anything after the comma is rounded. )

How do I get the 3001 value back from the textfield correctly?

Here is the examples of the various outputs as requested:

print(mytextfield.integerValue) //3
print(mytextfield.intValue) //3
print(mytextfield.doubleValue) //3.0
print(mytextfield.stringValue) //3,001

Upvotes: 0

Views: 43

Answers (1)

rmaddy
rmaddy

Reputation: 318854

The proper way to convert a user entered string into a number is to use NumberFormatter. This will properly handle locale specific grouping and decimal separators.

Methods such as integerValue or doubleValue do not support localized or formatted values.

Upvotes: 3

Related Questions