Convert Number String to a Different Region Format
up vote
0
down vote
favorite
I currently have a textfield that takes an amount of a certain currency and whatever the user enters into the text field (I.E 500.00) is sent up to the server for validation before preceding.
The problem is that when the user has their iPhone region set to somewhere like France where numbers are formatted like 4 567,89 instead of 4,567.89, the validation on the backend misconstrues and will think the provided number is different from what it actually is.
Therefore I have a String in the format of 4 567,89 and I want to convert it to the format 4,567.89 or 4567.89
I have tried using a NumberFormatter with a given Locale but it can't recognise 4 567,89 as a number and fails.
This is what I tried:
extension String {
func toEnglishNumberFormat() -> String? {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = false
currencyFormatter.numberStyle = NumberFormatter.Style.decimal
currencyFormatter.locale = Locale(identifier: "en_US_POSIX")
if let amount = currencyFormatter.number(from: self) {
return "(amount)"
} else {
return nil
}
}
}
Except this fails as currencyFormatter.number(from: self) cannot create a number from a string that looks like "4 567,89" or similar.
Anyway any ideas about how to handle numbers formatted in different regions so it won't fail validation? Thanks.
ios swift
add a comment |
up vote
0
down vote
favorite
I currently have a textfield that takes an amount of a certain currency and whatever the user enters into the text field (I.E 500.00) is sent up to the server for validation before preceding.
The problem is that when the user has their iPhone region set to somewhere like France where numbers are formatted like 4 567,89 instead of 4,567.89, the validation on the backend misconstrues and will think the provided number is different from what it actually is.
Therefore I have a String in the format of 4 567,89 and I want to convert it to the format 4,567.89 or 4567.89
I have tried using a NumberFormatter with a given Locale but it can't recognise 4 567,89 as a number and fails.
This is what I tried:
extension String {
func toEnglishNumberFormat() -> String? {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = false
currencyFormatter.numberStyle = NumberFormatter.Style.decimal
currencyFormatter.locale = Locale(identifier: "en_US_POSIX")
if let amount = currencyFormatter.number(from: self) {
return "(amount)"
} else {
return nil
}
}
}
Except this fails as currencyFormatter.number(from: self) cannot create a number from a string that looks like "4 567,89" or similar.
Anyway any ideas about how to handle numbers formatted in different regions so it won't fail validation? Thanks.
ios swift
It seems you are trying to manipulate the formatted string for your calculations. Don't do that! When the user has finished typing and you read the value from the text field, use thatString
to either convert toDouble
orNSNumber
. Send thatDouble
/NSNumber
to your server. Only apply the transformation fromDouble
/NSNumber
to formatted string when you show them to the user. Again, don't use the formatted string for your calculations, it's only for the visual representation convenient to the user.
– nayem
Nov 5 at 3:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I currently have a textfield that takes an amount of a certain currency and whatever the user enters into the text field (I.E 500.00) is sent up to the server for validation before preceding.
The problem is that when the user has their iPhone region set to somewhere like France where numbers are formatted like 4 567,89 instead of 4,567.89, the validation on the backend misconstrues and will think the provided number is different from what it actually is.
Therefore I have a String in the format of 4 567,89 and I want to convert it to the format 4,567.89 or 4567.89
I have tried using a NumberFormatter with a given Locale but it can't recognise 4 567,89 as a number and fails.
This is what I tried:
extension String {
func toEnglishNumberFormat() -> String? {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = false
currencyFormatter.numberStyle = NumberFormatter.Style.decimal
currencyFormatter.locale = Locale(identifier: "en_US_POSIX")
if let amount = currencyFormatter.number(from: self) {
return "(amount)"
} else {
return nil
}
}
}
Except this fails as currencyFormatter.number(from: self) cannot create a number from a string that looks like "4 567,89" or similar.
Anyway any ideas about how to handle numbers formatted in different regions so it won't fail validation? Thanks.
ios swift
I currently have a textfield that takes an amount of a certain currency and whatever the user enters into the text field (I.E 500.00) is sent up to the server for validation before preceding.
The problem is that when the user has their iPhone region set to somewhere like France where numbers are formatted like 4 567,89 instead of 4,567.89, the validation on the backend misconstrues and will think the provided number is different from what it actually is.
Therefore I have a String in the format of 4 567,89 and I want to convert it to the format 4,567.89 or 4567.89
I have tried using a NumberFormatter with a given Locale but it can't recognise 4 567,89 as a number and fails.
This is what I tried:
extension String {
func toEnglishNumberFormat() -> String? {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = false
currencyFormatter.numberStyle = NumberFormatter.Style.decimal
currencyFormatter.locale = Locale(identifier: "en_US_POSIX")
if let amount = currencyFormatter.number(from: self) {
return "(amount)"
} else {
return nil
}
}
}
Except this fails as currencyFormatter.number(from: self) cannot create a number from a string that looks like "4 567,89" or similar.
Anyway any ideas about how to handle numbers formatted in different regions so it won't fail validation? Thanks.
ios swift
ios swift
asked Nov 5 at 2:06
Cameron Porter
787
787
It seems you are trying to manipulate the formatted string for your calculations. Don't do that! When the user has finished typing and you read the value from the text field, use thatString
to either convert toDouble
orNSNumber
. Send thatDouble
/NSNumber
to your server. Only apply the transformation fromDouble
/NSNumber
to formatted string when you show them to the user. Again, don't use the formatted string for your calculations, it's only for the visual representation convenient to the user.
– nayem
Nov 5 at 3:13
add a comment |
It seems you are trying to manipulate the formatted string for your calculations. Don't do that! When the user has finished typing and you read the value from the text field, use thatString
to either convert toDouble
orNSNumber
. Send thatDouble
/NSNumber
to your server. Only apply the transformation fromDouble
/NSNumber
to formatted string when you show them to the user. Again, don't use the formatted string for your calculations, it's only for the visual representation convenient to the user.
– nayem
Nov 5 at 3:13
It seems you are trying to manipulate the formatted string for your calculations. Don't do that! When the user has finished typing and you read the value from the text field, use that
String
to either convert to Double
or NSNumber
. Send that Double
/NSNumber
to your server. Only apply the transformation from Double
/NSNumber
to formatted string when you show them to the user. Again, don't use the formatted string for your calculations, it's only for the visual representation convenient to the user.– nayem
Nov 5 at 3:13
It seems you are trying to manipulate the formatted string for your calculations. Don't do that! When the user has finished typing and you read the value from the text field, use that
String
to either convert to Double
or NSNumber
. Send that Double
/NSNumber
to your server. Only apply the transformation from Double
/NSNumber
to formatted string when you show them to the user. Again, don't use the formatted string for your calculations, it's only for the visual representation convenient to the user.– nayem
Nov 5 at 3:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I think you should convert from string of whatever locale is current on their device, into an NSNumber. Then convert the NSNumber to en_US formatted number string as that's what your webservice expects.
extension String
{
func toEnglishNumberFormat() -> String?
{
let numFormatter = NumberFormatter() // defaults to current locale
numFormatter.numberStyle = NumberFormatter.Style.currency
numFormatter.isLenient = true // will permit the spaces as separators
guard let num = numFormatter.number(from: self) else
{
return nil
}
numFormatter.locale = Locale(identifier: "en_US_POSIX")
numFormatter.numberStyle = NumberFormatter.Style.decimal
let resultStr = numFormatter.string(from: num)
return resultStr
}
}
I tested this while simulator was in French locale:
let str = "4 567,89"
let engStr = str.toEnglishNumberFormat()
print(engStr ?? "nil")
prints: 4567.89
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think you should convert from string of whatever locale is current on their device, into an NSNumber. Then convert the NSNumber to en_US formatted number string as that's what your webservice expects.
extension String
{
func toEnglishNumberFormat() -> String?
{
let numFormatter = NumberFormatter() // defaults to current locale
numFormatter.numberStyle = NumberFormatter.Style.currency
numFormatter.isLenient = true // will permit the spaces as separators
guard let num = numFormatter.number(from: self) else
{
return nil
}
numFormatter.locale = Locale(identifier: "en_US_POSIX")
numFormatter.numberStyle = NumberFormatter.Style.decimal
let resultStr = numFormatter.string(from: num)
return resultStr
}
}
I tested this while simulator was in French locale:
let str = "4 567,89"
let engStr = str.toEnglishNumberFormat()
print(engStr ?? "nil")
prints: 4567.89
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
add a comment |
up vote
1
down vote
accepted
I think you should convert from string of whatever locale is current on their device, into an NSNumber. Then convert the NSNumber to en_US formatted number string as that's what your webservice expects.
extension String
{
func toEnglishNumberFormat() -> String?
{
let numFormatter = NumberFormatter() // defaults to current locale
numFormatter.numberStyle = NumberFormatter.Style.currency
numFormatter.isLenient = true // will permit the spaces as separators
guard let num = numFormatter.number(from: self) else
{
return nil
}
numFormatter.locale = Locale(identifier: "en_US_POSIX")
numFormatter.numberStyle = NumberFormatter.Style.decimal
let resultStr = numFormatter.string(from: num)
return resultStr
}
}
I tested this while simulator was in French locale:
let str = "4 567,89"
let engStr = str.toEnglishNumberFormat()
print(engStr ?? "nil")
prints: 4567.89
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think you should convert from string of whatever locale is current on their device, into an NSNumber. Then convert the NSNumber to en_US formatted number string as that's what your webservice expects.
extension String
{
func toEnglishNumberFormat() -> String?
{
let numFormatter = NumberFormatter() // defaults to current locale
numFormatter.numberStyle = NumberFormatter.Style.currency
numFormatter.isLenient = true // will permit the spaces as separators
guard let num = numFormatter.number(from: self) else
{
return nil
}
numFormatter.locale = Locale(identifier: "en_US_POSIX")
numFormatter.numberStyle = NumberFormatter.Style.decimal
let resultStr = numFormatter.string(from: num)
return resultStr
}
}
I tested this while simulator was in French locale:
let str = "4 567,89"
let engStr = str.toEnglishNumberFormat()
print(engStr ?? "nil")
prints: 4567.89
I think you should convert from string of whatever locale is current on their device, into an NSNumber. Then convert the NSNumber to en_US formatted number string as that's what your webservice expects.
extension String
{
func toEnglishNumberFormat() -> String?
{
let numFormatter = NumberFormatter() // defaults to current locale
numFormatter.numberStyle = NumberFormatter.Style.currency
numFormatter.isLenient = true // will permit the spaces as separators
guard let num = numFormatter.number(from: self) else
{
return nil
}
numFormatter.locale = Locale(identifier: "en_US_POSIX")
numFormatter.numberStyle = NumberFormatter.Style.decimal
let resultStr = numFormatter.string(from: num)
return resultStr
}
}
I tested this while simulator was in French locale:
let str = "4 567,89"
let engStr = str.toEnglishNumberFormat()
print(engStr ?? "nil")
prints: 4567.89
edited Nov 5 at 3:32
answered Nov 5 at 2:30
Smartcat
1,6241315
1,6241315
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
add a comment |
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
Note I made several edits to my answer, but I believe I finally reached the exact answer you were looking for.
– Smartcat
Nov 5 at 3:34
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147408%2fconvert-number-string-to-a-different-region-format%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It seems you are trying to manipulate the formatted string for your calculations. Don't do that! When the user has finished typing and you read the value from the text field, use that
String
to either convert toDouble
orNSNumber
. Send thatDouble
/NSNumber
to your server. Only apply the transformation fromDouble
/NSNumber
to formatted string when you show them to the user. Again, don't use the formatted string for your calculations, it's only for the visual representation convenient to the user.– nayem
Nov 5 at 3:13