aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes Round number to nearest quarter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Round number to nearest quarter" Watch "Round number to nearest quarter" New topic

Round number to nearest quarter

Lauren Pender

Joined: Jun 15, 2001
Posts: 5
I have a bunch of fields and I would like to round them to the nearest .25. So if you enter 1.04 it rounds it to 1.0. If you have 1.23, it rounds it to 1.25. 1.65 to 1.75.
Thank you
Lauren Pender

Joined: Jun 15, 2001
Posts: 5
I forgot to say that this is in javascript.
Chris Treglio
Ranch Hand

Joined: Jun 18, 2001
Posts: 64
You can do this with the modulo (%) operator, like this
function round(value, increment) {
// value will be the number we'll round
// increment will be the 'round to' - in your case, .25
var remain;
var roundvalue;
var result;
remain = value % increment; // this will be somewhere between 0 and .25
roundvalue = increment / 2;

if (remain >= roundvalue){ // rounding up
result = value - remain;
result += increment;
else { // rounding down
result = value - remain;
return result;
I think that will do it. I didn't really run this through the paces with all the browsers, but I think that's it.
I agree. Here's the link:
subject: Round number to nearest quarter