I need to format (and not round off) a double
to 2 decimal places.
I tried with:
String s1 = "10.126";
Double f1 = Double.parseDouble(s1);
DecimalFormat df = new DecimalFormat(".00");
System.out.println("f1"+df.format(f1));
Result:
10.13
But I require the output to be 10.12
Why not use BigDecimal
If all you want to do is truncate a string at two decimal places, consider using just String functions as shown below:
This saves on parsing into a Double and then formatting back into a String.
Call
setRoundingMode
to set theRoundingMode
appropriately:Output
You can set the rounding mode of the formatter to DOWN:
Have you tried RoundingMode.FLOOR?