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,034and09are being treated as octal numbers because they start with0and you didn’t specify a base.8and9are invalid digits, so you get2and0for the first and last, and the second one is converted to decimal3*8 + 4 = 28.Use
parseInt(kg,10);