user4965201
user4965201

Reputation: 973

Javascript: Round Up to the Next Tens Place

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:

Upvotes: 0

Views: 516

Answers (2)

Addison Crump
Addison Crump

Reputation: 696

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);

Upvotes: 2

OliverRadini
OliverRadini

Reputation: 6467

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

Upvotes: 5

Related Questions