本文介绍了Javascript:四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数字四舍五入到100.

I'm trying to round a number the 100.

示例:

1340 should become 1400
1301 should become 1400

298 should become 300
200 should stay   200

我了解 Math.round ,但是它不能四舍五入到100.

I know about Math.round but it doesn't round to the 100.

我该怎么做?

推荐答案

尝试一下...

function roundUp(value) {
    return (~~((value + 99) / 100) * 100);
}

这将舍入到下一个百位-101将返回200.

That will round up to the next hundred - 101 will return 200.

jsFiddle示例- http://jsfiddle.net/johncmolyneux/r8ryd/

jsFiddle example - http://jsfiddle.net/johncmolyneux/r8ryd/

打开控制台以查看结果.

Open your console to see the results.

这篇关于Javascript:四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:51