Working with a Web-Application based on Java/Spring and FTL / JSP on the Template side you might know that you got some build in localization support(e.g. for number formatting). Various locales(java example locales). result in different number formatting:
- de_DE,german - 1.000,30 €
- cs_CZ,czech - 1 000,30 Kč
- en_GB,english - £1,000.30
- ...
Sometimes this is not that great
Currently some external Affiliate network requires us to handover prices as a standard float number (e.g: 1000.3) via their tracking code snippet.
How you should deal with that
--> Similar question on the Freemarker Templating Q&A
they are differentiating between "computer audience and human audience" in our example ${someLocalizedNumber?c}
will do the trick(Freemarker c builtin reference ) This takes the plain float below all (Spring) Localization and exposes this again as an String.
If you can't do that - Javascript to the help!
Normalize the Delimiter with Javascript
So i wrote a little script that deals with that issue and gets you your floaty number back:
function normalizeDelimiter(str){
var n = "", delim = false;
for (var i = str.length-1; i >= 0; i--){
//preserve decimal delimiter - remove all others
if(!delim && !isInt(str[i])){
n = "." + n;
delim = true;
}else{
n= (isInt(str[i]) ? str[i] : "") + n;
}
}
return parseFloat(n);
}
Normalize Delimiter takes a String that contains a (valid) Number and transform into a float.
Feed it with e.g.
'1.234,56'
'1 234,56'
'1#000#01'
'0.2'
'22'
Run it
normalizeDelimiter('1.234,56')
and you get:
1234.56
1234.56
1000.01
0.2
22
It handles the normal stuff as well as the ones that have even strange delimiters
To make use of the code you'll need the isInt() function
// check if given input is of type integer
function isInt(input){
return !isNaN(input)&&parseInt(input)==input;
}
Demo:
All assembled and ready to run @ jsFiddle
http://jsfiddle.net/9w5AQ/