Convert Currency String to FloatValue

0

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

"10,000,000"

    
asked by anonymous 17.08.2016 / 17:01

1 answer

1

Steps:

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

To do in a row 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: link

    
17.08.2016 / 17:18