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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















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












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






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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






share|improve this answer























  • 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















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






share|improve this answer























  • 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













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini