Carleto
Carleto

Reputation: 951

Sum currency from different locale in JavaScript

I have a question about this problem. I'm developing a form and it contains some currencies from 3 locales.I'm a Java developer, so I don’t have many skills about JS.And in this form i have 3 dynamic fields and when i change the value of taxes fields this 3 fields are changed to new value.In Java/Spring is easy to do that, with LocaleContextHoster and many other helper class, but in JS I dont have idea how to make this,because some countries use a decimal separator (.) and other use (,)

PS.: I don't want to use AJAX for this,I'm trying to do that in JS. But it is an alternative.

Upvotes: 0

Views: 208

Answers (1)

KooiInc
KooiInc

Reputation: 122936

Here's a helper method to convert a numeric string to a number:

function tryParse(numstr){
    var num_ = numstr.split(/\.|,/);
    numstr = num_.slice(0,-1).join('') + '.' + num_.slice(-1);
    return Number(numstr);
}
//e.g.
tryParse('200,30');       //=> 200.3
tryParse('1,000,121.47'); //=> 1000121.47
tryParse('1.000.121,47'); //=> 1000121.47

JsFiddle

Upvotes: 1

Related Questions