admin管理员组

文章数量:1178553

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.

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.

Share Improve this question edited Jan 30, 2012 at 9:03 Jamiec 136k15 gold badges141 silver badges199 bronze badges asked Jan 30, 2012 at 9:01 DaliDali 7,88228 gold badges114 silver badges178 bronze badges 1
  • possible duplicate of Round money to nearest 10 dollars in Javascript, take out the money factor and the fact that question is for tens instead of hundreds and the question is the same. Even the answers are identical. – Andy E Commented Jan 30, 2012 at 9:06
Add a comment  | 

4 Answers 4

Reset to default 25
function roundHundred(value){
   return Math.round(value/100)*100
}

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

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

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

we can use Math.ceil for doing this.

var rawNumber = 10651.89;
 roundFigure= Math.ceil(rawNumber /100)*100
function(x) {
  return Math.round(x / 100) * 100;
}

本文标签: Round to hundreds with javascriptStack Overflow