JSFiddle - React, Tailwind, and code Playground
HTML
<span id="log"></span>
<span id="result"></span>
JavaScript
// check if given input is of type integer
function is_int(input){
return !isNaN(input)&&parseInt(input)==input;
}
/* function normalizeDelimiter
* normalizes number delimiter differences to the format 1234.56 without rounding
* @require str String a String that contains a Number (e.g. '1.234,56', '1 234,56', '0.2')
* @return n Float a Number of Type Float (e.g. '1234.56', '1234.56', '0.2'
*/
function normalizeDelimiter(str){
var n = "", delim = false;
for (var i = str.length-1; i >= 0; i--){
//preserve decimal delimiter - remove all others
if(!delim && !is_int(str[i])){
n = "." + n;
delim = true;
}else{
n= (is_int(str[i]) ? str[i] : "") + n;
}
}
return parseFloat(n);
}
// run the normalization of the testdata
var prices_b = [] , prices = ['1990.30', '1990,00', '1 990,34', '1.990,00', '1-990,00' ,'1 990.00', '1,990.00',
'979,00', '979.00', '9,00', '9.00', '0,90', '0.90',
'1990', '979', '9', '1,990 01', '0.2'];
for (var p in prices){
prices_b.push(normalizeDelimiter(prices[p]));
}
// view test result
var result = "";
for(var i in prices){result += prices[i] + " -> " + prices_b[i] + " <br> "; }
$("#result").html(result);