0

What JavaScript formula can I use to truncate a number to the nearest 50. Example. I wanted 498 > 450

I have tried

Math.round (498, 50 )

And Math.ceil(498, 50)

But am not getting. Please help

  • Does this answer your question? [How do you round to 1 decimal place in Javascript?](https://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript) – jabaa Apr 14 '22 at 13:37
  • [`Math.round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) has one parameter. – jabaa Apr 14 '22 at 13:38
  • 3
    450 is not the "nearest" multiple of 50 to 498. What do you really want? To round *down* to the closest *smaller* multiple of 50? – Pointy Apr 14 '22 at 13:38
  • In general to round down to a multiple of a number n, divide n by your range, floor the result, and multiply by the range again. – Pointy Apr 14 '22 at 13:39

5 Answers5

3

This may be a mixup of terminology, mixing terms like "nearest" and "truncate", neither of which quite describes what the example demonstrates.

The example you give always rounds down, never up, to the nearest custom value (in this case 50). To do that you can just subtract the result of % 50. For example:

const val = 498;
console.log(val - val % 50);

Even make it a re-usable function:

const Nearest = (val, num) => val - val % num;

console.log(Nearest(498, 50));
David
  • 208,112
  • 36
  • 198
  • 279
1

Divide by 50, do the operation, multiply by 50.

console.log(Math.floor(498 / 50) * 50);
console.log(Math.ceil(498 / 50) * 50);
console.log(Math.round(498 / 50) * 50);
console.log(Math.trunc(498 / 50) * 50);
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
0

You divide your number by 50, take the ceiling of that number and then multiply it by 50.

Math.ceil(value / 50) * 50;

A quick sidenote: truncate has a whole other meaning for numbers in Javascript: Math.trunc on MDN

Edit:

If you want other rounding semantics than ceil you can of course use floor (always goes to lowest multiple of 50):

Math.floor(451 / 50) * 50; // => 450
Phillip
  • 6,033
  • 3
  • 24
  • 34
0

You divide by the multiple and round and then multiply by the multiple. If you want the lower bound, you use floor instead of round. If you want the upper bound, you use ceil instead of round. Look at these examples:

let x = 498;

let y = Math.round(498/50)*50;
console.log(y);

y = Math.floor(498/50)*50;
console.log(y);

y = Math.ceil(498/50)*50;
console.log(y);
Chris Strickland
  • 3,388
  • 1
  • 16
  • 18
0

To do what you want, the Remainder operator is your best friend. This will give you whatever is left over after dividing the number by the nearest number.

If your goal is to always round down, the following function would work. Just take your original number, find the remainder, and remove the remainder:

function roundDownToNearest(num, nearest){
    return num - (num % nearest);
}

console.log(roundDownToNearest(498, 50))

If you always want to round up, you round down, then add the nearest amount:

function roundUpToNearest(num, nearest){
    return num - (num % nearest) + nearest;
}

console.log(roundUpToNearest(498, 50))

If you want to get to the closest of the two, you could do the following. Find your remainder, then see if it's greater or less than half of your nearest value. If it's greater, round up. If less, round down.

function roundToNearest(num, nearest){
     if(num % nearest > nearest / 2){
          return roundUpToNearest(num, nearest);
     } else {
          return roundDownToNearest(num, nearest);
     }
}

console.log(roundToNearest(498, 50))
console.log(roundToNearest(458, 50))
Itinerati
  • 922
  • 2
  • 9
  • 19