-1

I would like to round numbers to the nearest 0.25-value like this:

2.1 -> 2
2.2 -> 2.25
3.4 -> 3.5
5.6 -> 5.5
9.8 -> 9.75
9.9 -> 10

Is this possible in javascript? Thank you in advance!

Bunwa
  • 11
  • 2

1 Answers1

3

Just multiply the number by 4, round it, and divide by 4:

[2.1, 2.2, 3.4, 5.6, 9.8, 9.8, 9.9].forEach(
  num => console.log(Math.round(num * 4) / 4)
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks that works! Also, now it seems this was asked before. I searched before asking but did not find what I was looking for back then. – Bunwa Oct 21 '18 at 08:17