javascript - When passing number with comma to text box why getElementById truncate the number after the comma -
please check code :
function calcpm() { var nv = document.getelementbyid('txtviews').value; var nc = document.getelementbyid('txtcost').value; var result =parsefloat(nc) / parsefloat(nv)/1000; if(!isnan(result)) { document.getelementbyid('cpm').value = result.tofixed(4); } }
it work fine expect when put comma in number, means if put in txtviews 1000000 , in txtcost 3000 correct result 3.000
however if use commas in of numbers problem starts, like
if put in txtviews 1,000,000 , in txtcost 3,000
then result 0.000003
or if start putting value txtcost first result 3000.0000
which not correct value either way, idea problem??
i believe you'd have strip them out, , add them in (the commas, is)..
function calcpm() { var nv = document.getelementbyid('txtviews').value.replace(',',''); var nc = document.getelementbyid('txtcost').value.replace(',',''); var result =parsefloat(nc) / parsefloat(nv)/1000; if(!isnan(result)) { document.getelementbyid('cpm').value = number(result).tolocalestring('en'); } }
Comments
Post a Comment