2

In JavaScript, I would like to round a given number (x) up based on another decimal (y). For example:

x = 8.6333
y = 0.5
result = 9

x = 8.6333
y = 0.2
result = 8.8

x = 8.6333
y = 0.1
result = 8.7

How would I go about doing this?

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Kyle Hayes
  • 5,225
  • 8
  • 38
  • 53

1 Answers1

3

Divide x by the y. Round that up, and multiply by y again.

var answer = Math.ceil(x/y)*y;
Rene Pot
  • 24,681
  • 7
  • 68
  • 92