javascript - JS - Numbers rounded in real time -
i created form enable users have quote:
<form name="autosumform"> <br><br> width: <input class="right" type="text" name="firstbox" value="" onblur="stopcalc();"><br> height: <input class="right" type="text" name="secondbox" value="" onblur="stopcalc();"><br> type: <select class="right" name="thirdbox" onfocus="startcalc();" onblur="stopcalc();"> <option selected="selected">-</option> <option value="7.80">forex bianco 3 mm</option> <option value="8.30">forex bianco 5 mm</option> <option value="9.90">forex bianco 10 mm</option> <option value="5.70">forex piuma 10 mm</option> </select><br> total cost: <input class="right" type="text" name="fourbox"> </form>
i wrote little js calculate total cost:
function startcalc(){ interval = setinterval("calc()",1); } function calc(){ 1 = document.autosumform.firstbox.value; 2 = document.autosumform.secondbox.value; 3 = document.autosumform.thirdbox.value; document.autosumform.fourbox.value = (one * 1) * (two * 1) * (0.0001) * (three * 1); } function stopcalc(){ clearinterval(interval); } function roundnumber(number, decimals){ var newnumber = new number(fourbox).tofixed(parseint(decimals)); document.autosumform.fourbox.value = parsefloat(newnumber); }
now problem is: every time fill form result number many digits after comma, tryed many things no 1 went good... please help! thank you!!!
ps know better server side type of work, it's simple site , every order processed person.
ok, i'm not 100% sure trying calculate here changes think may trying achieve, if not may @ least set in right direction. btw, better use "id" rather "name" identifying elements "id" must unique.
html
<form name="autosumform"> <br> <br>width: <input class="right" type="text" name="firstbox" value=""> <br>height: <input class="right" type="text" name="secondbox" value=""> <br>type: <select class="right" name="thirdbox"> <option selected="selected">-</option> <option value="7.80">forex bianco 3 mm</option> <option value="8.30">forex bianco 5 mm</option> <option value="9.90">forex bianco 10 mm</option> <option value="5.70">forex piuma 10 mm</option> </select> <br>total cost: <input class="right" type="text" name="fourbox" value="0.00"> </form>
javascript
var firstbox = document.queryselector("[name='firstbox']"); var secondbox = document.queryselector("[name='secondbox']"); var thirdbox = document.queryselector("[name='thirdbox']"); var fourbox = document.queryselector("[name='fourbox']"); function update() { fourbox.value = ((parsefloat(firstbox.value) * parsefloat(secondbox.value) * 0.0001 * parsefloat(thirdbox.value)) || 0).tofixed(2); } firstbox.addeventlistener("keyup", update, false); secondbox.addeventlistener("keyup", update, false); thirdbox.addeventlistener("change", update, false);
on jsfiddle
wrt comment parsefloat vs unary plus operator
it possible unary plus operator faster in case (when?), on 2 of popular browsers (chrome , firefox), jsperf shows parsefloat faster
i think parsefloat shows intent far clearer pre-pending "+", shorter code not better code
there differences between 2 methods when converting string, consider jsfiddle
Comments
Post a Comment