如何在 Java 中舍入 *down* 整数?

2022-01-17 00:00:00 numbers java

我想在 Java 中将整数向下舍入到最接近的 1000.

I'd like to round integers down to their nearest 1000 in Java.

例如:

  • 13,623 轮到 13,000 轮
  • 18,999 轮到 18,000 轮

推荐答案

简单地除以1000去掉你不感兴趣的数字,再乘以1000:

Simply divide by 1000 to lose the digits that are not interesting to you, and multiply by 1000:

i = i/1000 * 1000

或者,你也可以试试:

i = i - (i % 1000)

相关文章