Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

round to nearest .25 javascript

I want to convert all numbers to the nearest .25

So...

5 becomes 5.00
2.25 becomes 2.25
4 becomes 4.00
3.5 becomes 3.50
like image 948
dotty Avatar asked Oct 12 '09 09:10

dotty


People also ask

How do you round decimals in JavaScript?

The Math. round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3).

How do you round 2 decimals in JavaScript?

Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.

How do you round to the nearest 10 in JavaScript?

round to Round a Number to the Nearest 10 in JavaScript. To round a number to the nearest 10 , you can call the Math. round() function, passing the number divided by 10 and multiplying the result by 10 , e.g., Math. round(num / 10) \* 10 .


7 Answers

Here’s an implementation of what rslite said:

var number = 5.12345;
number = (Math.round(number * 4) / 4).toFixed(2);
like image 124
Gumbo Avatar answered Oct 04 '22 10:10

Gumbo


Multiply by 4, round to integer, divide by 4 and format with two decimals.

like image 24
rslite Avatar answered Oct 04 '22 10:10

rslite


If speed is your concern, note that you can get about a 30% speed improvement by using:

var nearest = 4;
var rounded = number + nearest/2 - (number+nearest/2) % nearest;

From my website: http://phrogz.net/round-to-nearest-via-modulus-division
Performance tests here: http://jsperf.com/round-to-nearest

like image 43
Phrogz Avatar answered Oct 04 '22 12:10

Phrogz


Here is a generic function to do rounding. In the examples above, 4 was used because that is in the inverse of .25. This function allows the user to ignore that detail. It doesn't currently support preset precision, but that can easily be added.

function roundToNearest(numToRound, numToRoundTo) {
    numToRoundTo = 1 / (numToRoundTo);

    return Math.round(numToRound * numToRoundTo) / numToRoundTo;
}
like image 21
cjbarth Avatar answered Oct 04 '22 12:10

cjbarth


Here is @Gumbo's answer in a form of a function:

var roundNearQtr = function(number) {
  return (Math.round(number * 4) / 4).toFixed(2);
};

You can now make calls:

roundNearQtr(5.12345); // 5.00
roundNearQtr(3.23); // 3.25
roundNearQtr(3.13); // 3.25
roundNearQtr(3.1247); // 3.00
like image 31
cosmixtew Avatar answered Oct 04 '22 11:10

cosmixtew


function roundToInc(num, inc) {
    const diff = num % inc;
    return diff>inc/2?(num-diff+inc):num-diff;
}

> roundToInc(233223.2342343, 0.01)
233223.23
> roundToInc(505, 5)
505
> roundToInc(507, 5)
505
> roundToInc(508, 5)
510
like image 23
Ben Avatar answered Oct 04 '22 10:10

Ben


Use below function, hope it helps

function roundByQuarter(value) {
    var inv = 1.0 / 0.25;
    return Math.round(value * inv) / inv;
}

Call the function as below, will result the nearest Quarter value, that is it will not return .32, .89, .56 but will return .25, .75, .50 decimals only.

roundByQuarter(2.74) = 2.75

roundByQuarter(2.34) = 2.25

roundByQuarter(2.94) = 3.00

roundByQuarter(2.24) = 2.25
like image 41
Mahesh Yadav Avatar answered Oct 04 '22 12:10

Mahesh Yadav