Wednesday, August 10, 2022

[FIXED] how to increment a decimal/float value by 0.1 or 0.001 according to degree of decimal value in java

Issue

I have a decimal number eg 65.435 or 9.2 or 10.23 , now i have to increment it as 65.436, 9.3,10.24.

Is there any utility method present for that in java?

I know we can do it by checking decimal place and then increment, but i want like in case of integer if we use '++' it increment the value.

Can anyone suggest a better way to handle.


Solution

spelling out Mr.Robot's comment:

BigDecimal bd = new BigDecimal("1.234");
bd = new BigDecimal(bd.unscaledValue().add(BigInteger.ONE), bd.scale());


Answered By - Reto Höhener
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.