Skip to content
Advertisement

JavaScript round number to first two digits?

I’m trying to round a number up to the first two digits.

JavaScript

I have figured out this much:

JavaScript

It returns 14000, but I want to round it up. Math.ceil only works to the next integer. http://jsfiddle.net/cruTY/

Note: I know I can divide n to make it a decimal, but I want this function to work for all numbers without having to manually insert /1000 or /1000000.

Advertisement

Answer

Here you go:

JavaScript

What’s your requirement if String(n).length < 2?

Alternately,

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement