How to round integer in java

后端 未结 12 821
野趣味
野趣味 2020-12-09 09:19

I want to round the number 1732 to the nearest ten, hundred and thousand. I tried with Math round functions, but it was written only for float and double. How to do this for

12条回答
  •  时光取名叫无心
    2020-12-09 09:37

    At nearest ten:

    int i = 1986;
    int result;
    
    result = i%10 > 5 ? ((i/10)*10)+10 : (i/10)*10;
    

    (Add zero's at will for hundred and thousand).

提交回复
热议问题