2
How can I convert the value of an input 1,11
for 1.11
, so it can be added to other values? This in jQuery.
My input is masked for real values.
2
How can I convert the value of an input 1,11
for 1.11
, so it can be added to other values? This in jQuery.
My input is masked for real values.
1
If you want to make calculations with the value of input
, then you need to convert them to number. The problem is that you can’t save the number back to a input
because it would go back to being a string (if I’m not mistaken, even the input
like number
in HTML5 still saves its value as 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 on input
string:
$(meuInput).val(valorComPonto);
Blz, I’m gonna try.
@Sergio Ops, yes, was with the question of monetary values in mind (where I always represent by integers, in cents) and forgot that in this case you are using floating point. Thanks!
Person, now I found a small problem in the sum. I have 7 input, that is, we are 7 values. The result is correct, but with 14 zeros and 1 one, after the result hehehehehehe Example: 7,77000000000000001 I am zeroing all variables, before they receive the values.
@Gustavosevero Hehe and is therefore that floating point should not be used for calculations with currency units. : ) Take a look at the question I mentioned in the comments, unfortunately there is no easy solution to this no.
i even managed to fix the problem by placing . toFixed(2) after the value variable. But thanks anyway.
1
You can do it with a line:
var valorParaCalculo = parseFloat(($(".oi").val()).replace(/\./g,"").replace(/,/g,".")); // input:999.999.999,99 - output: 999999999.99
Example in the fiddle: http://jsfiddle.net/zskq7cf6/1/
I’ll try that tip you gave.
I got @Joao Paulo. I made some changes only of variable name, but I got it. Hallelujah!!!
In fact what I did to test if I calculated, was to duplicate the input of your code, change the values, of course and I made a simple sum with the two values and it worked. It’s funny, nowadays misery is made and there are any frameworks in JS and to make a simple calculation, we catch hehehehehehe
now I found a small problem in the sum. I have 7 input, that is, we are 7 values. The result is correct, but with 14 zeros and 1 one, after the result hehehehehehe Example: 7,77000000000000001 .
Browser other questions tagged jquery html5
You are not signed in. Login or sign up in order to post.
"to be added to other values" that means you don’t want to simply convert from string to string, but to number as well, right? And your input string has some point (like:
1.234,56
) or just a comma? (it is easier if there is no point in the value)– mgibsonbr
@mgibsonbr, Yes, that’s it, from string to number or monetary value. Face, can be values 0.01 up to 1,000,000.00
– GustavoSevero
Related: "How to represent money in Javascript?"
– mgibsonbr
@mgibsonbr, the link above helped little, but did not resolve hehehe
– GustavoSevero
JQ = jQuery? Because there is http:/stedolan.github.io/jq/ which is something else.
– Marcus Vinicius