quick javascript Q
- Started
- Last post
- 10 Responses
- stewart
how do i "round off" numbers entered in a textfield using javascript?
example:
in a textfield i type "160", and when i change the cursor to another textfield "160" becomes "200"...(i need to round off the number in hundreds in this case)
- REINVIGORATE0
You may be looking for something a little more complex but here's my stab at it-
function rounder(n, b)
{
return Math.ceil(n/b)*b;
}
// 160 rounded to the nearest 100th
document.write(rounder(160,100... // 200
- stewart0
thanks, that already helped me a lot.
but Math.ceil(n/b)*b is rounding off "110" to "200" too.
trying to think of a Math that rounds off
140 to 100
150 to 200
210 to 200
etc...
- UndoUndo0
Math.floor(); is yr friend
hows things reinvigorate? what ever happened to the site?
- REINVIGORATE0
I think Math.round(n/b)*b should do the trick if you want it rounding on half widths.
- stewart0
Math.floor isn't my friend in this.
I'm calculating my brains off for a solution...
- REINVIGORATE0
Undo- It's good. I'm still plugging away at it.
- REINVIGORATE0
Did trying Math.random work out ok?
- UndoUndo0
sorry yr right stewart math.round() is what you want
Math.floor() takes the number down to lowest whole integer
ooops ;)
- stewart0
Ah.
Spank y'all very much!