四舍五入到最接近的100最接近、四舍五入

2023-09-11 00:06:04 作者:无人街角

第一个数字需要被四舍五入到最接近的第二个数字。这样做有很多方法,但最新最好的和最短的算法?任何人都为一个挑战:-)

First number needs to be rounded to nearest second number. There are many ways of doing this, but whats the best and shortest algorithm? Anyone up for a challenge :-)

1244-> 1200 1254-> 1300 123-> 100 178-> 200 1576-> 1600 1449-> 1400 123456-> 123500 654321-> 654300 23-> 00 83-> 100

1244->1200 1254->1300 123->100 178->200 1576->1600 1449->1400 123456->123500 654321->654300 23->00 83->100

推荐答案

对于输入 N

(n + 50) / 100 * 100

使用整数除法。

using integer division.

请注意,很多语言/库已经具备的功能来做到这一点。

Note that many languages/libraries already have functions to do this.