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.
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.
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
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);