Dali
Dali

Reputation: 7882

Round to hundreds with javascript

I want to round numbers to hundreds with javascript like this:

10651.89    = 10700
10649.89    = 10600
60355.03    = 60400
951479.29   = 951500
1331360.95  = 1331400

How can I do that ?

Thanks a lot.

Upvotes: 12

Views: 13080

Answers (4)

VISHNU
VISHNU

Reputation: 966

we can use Math.ceil for doing this.

var rawNumber = 10651.89;
 roundFigure= Math.ceil(rawNumber /100)*100

Upvotes: 4

AustinDahl
AustinDahl

Reputation: 852

function(x) {
  return Math.round(x / 100) * 100;
}

Upvotes: 2

Jamiec
Jamiec

Reputation: 136114

function roundHundred(value){
   return Math.round(value/100)*100
}

Live example with your test cases: http://jsfiddle.net/LaPGs/

Upvotes: 25

qiao
qiao

Reputation: 18219

you can divide it by 100 first then use Math.round, and finally multiply it by 100.

> Math.round(10651.89 / 100) * 100
10700

Upvotes: 5

Related Questions