How to round integer in java

后端 未结 12 842
野趣味
野趣味 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:53

    why not just check the unit digit... 1. if it is less than or equal to 5, add 0 at the unit position and leave the number as it is. 2. if it is more than 5, increment the tens digit, add 0 at the unit position.

    ex: 1736 (since 6 >=5) the rounded number will be 1740. now for 1432 (since 2 <5 ) the rounded number will be 1430....

    I hope this will work... if not than let me know about those cases...

    Happy Programming,

提交回复
热议问题