European and US formatted currency
October 20, 2008
I've just been trying to help out some guys and gals over on the CF Adobe forums (I try and give something back occasionally). Here's a question that I've just answered:
I have a question I need to convert a European number (. as thousand separator and a , as decimal separator) to an American number. But I could receive both type of numbers. So when I get an American number delivered I want to do nothing and in case of an European number I want to convert it.
When I have a number say 123.456.789,01 I want to transfer it to 123,456,789.01
I wrote a little function to handle converting European and US formatted currency to a decimal, but I'm sure there is a neater (pure CF) solution. Anyway here is what I came up with:
<cfscript>
function ConvertToNumber(s) {
var parsed = ListChangeDelims( arguments.s, ".", ",.'" );
var integer = "";
var decimal = "";
if (ListLen( parsed, "." ) gt 1) {
integer = Replace( Left( parsed, parsed.lastIndexOf(".") ), ".", "", "all" );
decimal = ListLast( parsed, "." );
return integer & "." & decimal;
}
else {
return parsed;
}
}
</cfscript>
<cfoutput>
ConvertToNumber("123.456.789,01") = #ConvertToNumber("123.456.789,01")#<br />
ConvertToNumber("123,456,789.01") = #ConvertToNumber("123,456,789.01")#<br />
ConvertToNumber("123,45") = #ConvertToNumber("123,45")#<br />
ConvertToNumber("123.45") = #ConvertToNumber("123.45")#<br />
ConvertToNumber("123456789") = #ConvertToNumber("123456789")#<br />
</cfoutput>
Feel free to put me to shame :)
- Posted in:
- ColdFusion
3 comments
Leave a comment
If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Comment by PaulH – October 20, 2008
I think numbers should be saved always without any dot or comma and formatted while displaying with required local settings.
Comment by Oğuz Demirkap? – October 21, 2008
I don't know where the value was coming from but I guess it is a form value. Totally agree that you should store as a straight decimal. I think the guy was trying to convert to a decimal so that he could save it in a database or format it with the inbuilt CF functions. My function just works out what is being used as the decimal point and strips out the thousand seperators.
@PaulH, here is an example of what I think he means by "European formatted currency":
www.achetezfacile.com/canon-eos-1d-mark-iii-comparer-les-prix-177747.html?gclid=CJjAsoHut5YCFQWc1AodIirWLQ
Comment by John Whish – October 21, 2008