The code:
import java.math.*;
public class x
{
public static void main(String[] args)
{
BigDecimal a = new BigDecimal(0.1);
BigDecimal b = new BigDecimal(0.7);
System.out.println(a);
System.out.println(b);
}
}
The output:
0.1000000000000000055511151231257827021181583404541015625
0.6999999999999999555910790149937383830547332763671875
This is nice because it lets me find a double that is the closest to given value.
But as for 0.1 a value is bigger and for 0.7 value is smaller than real value.
How can I get both values (closest bigger and closest smaller) for any decimal number?
Assume I start with BigDecimal, then convert it do a double and then back to decimal. I’ll get bigger or smaller value. How could I got the other?
Use nextAfter(double start, double direction):
Printout: