0

I have a javascript formula that rounds off the input number number.

var number = x;

alert (Math.round(number / 10) * 10);

This will output 11,12,13,14 as 10 and 15,16,17 as 20.

This is not what I want; I want to add 1 to 10 (11) and round it up, which will return/print 20.

Example:

  • 11, 12 ,13, 14... 19 rounds to 20
  • 21, 22... 29 rounds to 30
  • etc.
Addison Crump
  • 666
  • 5
  • 19
user4965201
  • 973
  • 1
  • 11
  • 25

2 Answers2

5

I think you could use Math.ceil instead of Math.round.

Matt Harrison
  • 13,381
  • 6
  • 48
  • 66
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
2

If what I'm interpreting your question as is indeed what you're asking, then you want Math.ceil instead.

This will equate to the ceiling of the function ("rounded up", if you will)

Therefore, your code becomes:

var number = x;

alert (Math.ceil(number / 10) * 10);
Addison Crump
  • 666
  • 5
  • 19