How to round *down* integers in Java?

前端 未结 3 2043
盖世英雄少女心
盖世英雄少女心 2021-01-01 14:30

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

So for example:

  • 13,623 rounds to 13,000
  • 18,999 rounds to 18,000
  • etc
3条回答
  •  孤城傲影
    2021-01-01 15:12

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

    i = i/1000 * 1000
    

    Or, you can also try:

    i = i - (i % 1000)
    

提交回复
热议问题