views:

274

answers:

2

I have a string representing a number, and I want to convert it an NSInteger. The problem is, that the string is formatted with thousand separators:

"1,234"

when using [value intValue], I get 1 as the value.

Is it because it thinks the thousand separator is a decimal separator? (my locale uses comma as decimal separator and a space or a dot as thousand separator)

How can I ensure that I get the right number?

-Vegar

+3  A: 

There's the NSNumberFormatter class which can not only encode numbers, but also decode them according to the current locale.

Georg
By setting the positive number format to @"#,##0", I get the right number. Thanks!
Vegar
+1  A: 

I would find a class that can decode numbers from strings just as Georg suggests, but a quick solution could also be to chop off the extra thousand separators using the stringByReplacing… method of the NSString class.

zoul