I am trying to fill (not a round!) Doubles to two digits in a Java application. I use DecimalFormat for this, but noticed that for negative values โโclose to zero, the values โโare not rounded to -0.01 , but to -0.00 .
public class MyTest { void formatAndPrint(double value) { DecimalFormat df = new DecimalFormat("0.00"); df.setRoundingMode(RoundingMode.FLOOR); System.out.println(value + " => " + df.format(value)); } @Test public void testFloor() { formatAndPrint(2.3289);
Other solutions, such as Math.floor(value * 100.0) / 100.0 , do not have this problem, but have other problems, for example, erroneous flooring 2.3 to 2.29 .
Is there a flooring solution for Java that works in all cases?
source share