rounding - How can I have the value round up in javascript? -
i have code inserting total value textbox, however, math performed not round number. based on code below how can make happen?
function calculate(){ var mrc = document.getelementbyid('box1'); var days = document.getelementbyid('box2'); var total = document.getelementbyid('box3'); var reason = document.getelementbyid('box4'); var approver = document.getelementbyid('box5'); var approvalreason = document.getelementbyid('box6'); var custname = document.getelementbyid('box7'); var caseid = document.getelementbyid('box8'); var intermitent = document.getelementbyid('rb1'); var outage = document.getelementbyid('rb2'); if (outage.checked === true) { if (days.value * 5 > mrc.value){ total.value = (mrc.value / 30) * days.value; } else if (days.value > 14) { total.value = (mrc.value / 30) * days.value; } else { total.value = days.value * 5; } } else if (intermitent.checked === true){ if (days.value * 3 > mrc.value) { total.value = (mrc.value / 30) * days.value; } else if (days.value > 14) { total.value = (mrc.value / 30) * days.value; } else { total.value = days.value * 3; } } }
two things:
you're playing fire using implicit type conversion. element.value
returns string
, not number
, should using parseint()
or parsefloat()
convert values numbers. instance, if input has value 3, , element.value + 2
, result 32
.
second, question, math.ceil()
rounds float near integer.
Comments
Post a Comment