NSNumberFormatter whitespace

I'm trying to remove all the whitespaces from the NSNumberFormatter string. It seems it adds Unicode 160 as a whitespace instead of Unicode 32 and then String's stringByReplacingOccurrencesOfString doesn't work. Is this correct behavior and how should I remove the whitespaces?


let formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "fi")
formatter.numberStyle = .CurrencyStyle
formatter.currencyCode = "EUR"
formatter.currencySymbol = ""

let value = formatter.stringFromNumber(1234) ?? "" //"1 234,00 "
let trimmed = value.stringByReplacingOccurrencesOfString(" ", withString: "") // "1 234,00 "

let chars = value.utf16.map { $0 }
chars // [49, 160, 50, 51, 52, 44, 48, 48, 160]
Answered by DTS Engineer in 47398022

The value you’re seeing is U+00A0 NO-BREAK SPACE. NSNumberFormatter uses this deliberately because word wrapping at a thousands separator would be very confusing.

To achieve your goal you should look at

+[NSCharacterSet whitespaceCharacterSet]
. There are various ways you can use this to search and replace for whitespace but here’s a one-line drop-in replacement.
let trimmed = value.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).joinWithSeparator("")

Finally, concerning your goal you wrote:

I'm trying to remove all the whitespaces from the NSNumberFormatter string.

This is worrying from a localisation standpoint. I don’t have any concrete examples but, given the wide world of locale-specific behaviour, my guess is that there will be at least one place where removing whitespace from a formatted number causes semantic differences.

Share and Enjoy

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

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

The value you’re seeing is U+00A0 NO-BREAK SPACE. NSNumberFormatter uses this deliberately because word wrapping at a thousands separator would be very confusing.

To achieve your goal you should look at

+[NSCharacterSet whitespaceCharacterSet]
. There are various ways you can use this to search and replace for whitespace but here’s a one-line drop-in replacement.
let trimmed = value.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).joinWithSeparator("")

Finally, concerning your goal you wrote:

I'm trying to remove all the whitespaces from the NSNumberFormatter string.

This is worrying from a localisation standpoint. I don’t have any concrete examples but, given the wide world of locale-specific behaviour, my guess is that there will be at least one place where removing whitespace from a formatted number causes semantic differences.

Share and Enjoy

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

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

Thanks, I'm removing whitespace before validating the input so it's not visible to the user.

How are you validating localised numbers? The only reasonable way to do that is to call NSDateFormatter itself, but that’ll happily ignore the whitespace for you. Validating numbers any other way is challenging (you have to deal with Eastern Arabic numerals, amongst other things).

Share and Enjoy

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

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

The app is limited to certain region but you are right. I should use NSNumberFormatter for validation also. Thanks!


E: Usually the input is without the currency code. Should I use DecimalStyle instead of CurrencyStyle because NSNumberFormatter does not recognize the number if it is without the currency code. For example


let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencyCode = "EUR"
let num1 = formatter.numberFromString("12345.68") // nil

Hello and how can i find a solution where i add to the amount the currency But i need to remove the spaces between numbers but keep the space between the numbers and currency

The suggested solution above transfered my code from 9 520 USD to 9520USD

What i want is 9520 USD

NSNumberFormatter whitespace
 
 
Q