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
  • 51,385
  • 12
  • 117
  • 139
Kyle Hayes
  • 5,078
  • 7
  • 34
  • 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,003
  • 7
  • 67
  • 90