i just encounter 1 problem on the parseInt i do apply this parseInt for all my project but today i just encounter this
i have a normal text input <input type='text' name='KG'>
and i using jquery to retrieve the input value, then write into another input
<input type='text' name='KG2'>
below are my jquery code
$(":input[name='KG']").keyup(calc);
function calc(){
var kg = $(":input[name='KG']").val();
kg=parseInt(kg);
$(":input[name='KG2']").val(kg);
}
guess what, this the result i get
input@KG > show@KG2
28 > 28
028 > 2
34 > 34
034 > 28
9 > 9
09 > 0
anyone know what went wrong? it can be solve by using Math.round.
028, 034 and 09 are being treated as octal numbers because they start with 0 and you didn't specify a base. 8 and 9 are invalid digits, so you get 2 and 0 for the first and last, and the second one is converted to decimal 3*8 + 4 = 28.
Use parseInt(kg,10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With