Converting values for calculation in JQ

2

How can I convert the value of an input 1,11 to 1.11 so it can be added to other values? This in jQuery.

My input has a mask for values in reais.

    
asked by anonymous 11.06.2015 / 22:31

2 answers

1

You can do with a line:

var valorParaCalculo =  parseFloat(($(".oi").val()).replace(/\./g,"").replace(/,/g,".")); // input:999.999.999,99 - output: 999999999.99

Example on fiddle: link

    
11.06.2015 / 23:24
1

If you want to make calculations with the value of input , then you need to convert them to the number. The problem is that you can not save the number back to input because it would return a string (if I'm not mistaken, even input with type number in HTML5 still stores its value as a string), you need to convert and use direct.

Each step is simple by itself. Together it would be:

var valorComVirgula = $(meuInput).val(); // "1.000.000,00"
var valorSemPonto = valorComVirgula.replace(/\./g, ""); // "1000000,00"
var valorComPonto = valorSemPonto.replace(/,/g, "."); // "1000000.00"
var valorNumerico = parseFloat(valorComPonto); // 1000000

And if you want to put the number back in input as string:

$(meuInput).val(valorComPonto);
    
11.06.2015 / 22:39