Rounding to nearest 100

前端 未结 7 806

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 :-)

7条回答
  •  春和景丽
    2020-12-15 04:58

    This will do it, given you're using integer math:

    n = (n + 50) / 100 * 100
    

    Of course, you didn't specify the behavior of e.g., 1350 and 1450, so I've elected to round up. If you need round-to-even, that'll not work.

提交回复
热议问题