I want to round this double:
3.499999999999999
to:
3.50
And I already used these two methods:
<
Your first solution is really, really close. Just do this:
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(input));
The reason your version doesn't work is that you're taking the input
double, formatting it as a String
with 2dp, but then converting it back to a double
. System.out.println
is then printing the double
as it always would, with no special decimal-place rules.