74

I want to round up to the nearest 100 all the time whether or not the value is 101 or 199 it should round up to 200. For example:

var number = 1233;
//use something like Math.round() to round up to always 1300

I'd like to always round up to the nearest 100, never round down, using jQuery.

4

5 回答 5

151

使用Math.ceil(),如果你想总是四舍五入:

Math.ceil(number/100)*100
于 2013-10-27T18:05:34.370 回答
22

要向上和向下舍入到最接近的 100,请使用Math.round

Math.round(number/100)*100

roundceil

Math.round(60/100)*100 = 100对比Math.ceil(60/100)*100 = 100

Math.round(40/100)*100 = 0对比Math.ceil(40/100)*100 = 100

Math.round(-60/100)*100 = -100对比Math.ceil(-60/100)*100 = -0

Math.round(-40/100)*100 = -0对比Math.ceil(-40/100)*100 = -0

于 2018-07-30T12:46:03.233 回答
9

这一切都不需要jQuery。只需使用 JavaScript 的Math.ceil

Math.ceil(x / 100) * 100
于 2013-10-27T18:05:02.683 回答
1

这是一个简单的方法:

((x/100).toFixed()*100;
于 2018-08-31T10:56:05.713 回答
1

最简单的解决方案是:

Math.round(number/100)*100
于 2021-09-03T09:12:10.603 回答