Convert Currency String to Floatvalue

Asked

Viewed 81 times

0

blz? How can I convert a currency string value (en) to a float value in javascript.

"10,000.50"

1 answer

1


Footsteps:

  • remove the points: .split('.').join('')
  • comma to point: .split(',').join('.')
  • float: Number('string');

To do on a line would only be:

Number("10.000,50".split('.').join('').split(',').join('.'));

In function:

function toFloat(str) {
    if (typeof str == 'number') return str;
    return Number(str.split('.').join('').split(',').join('.'));
}

var testes = ['10.000,50', '105,45', '1.000.005,70'];
console.log(JSON.stringify(testes.map(toFloat))); // [10000.5,105.45,1000005.7]

jsFiddle: https://jsfiddle.net/67Lmhhe1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.