Good day.
I need to format a number in java.
So far I have this:
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
System.out.println(new Double(df2.format(balance)).doubleValue());
But it prints out this
110.0
121.0
133.1
146.41
161.05
But I need it to be with two digits in fraction part. How do I do it?
You don’t have to get double value from formatted string.
Just use formatted string, which is returned from
format()method ofDecimalFormat.So your code should be like the following:
Your original code:
What you did in your code is: format the double value to string(which is formatted as you specified in the
DecimalFormatinstance). Then you convert the formatted string toDoubleinstance and get double value from the object, which is double. And then printed it to console. So the formatted string is gone, and the double value is printed as normal.