No, there is no better way.

Actually you have an error in your pattern. What you want is:

DecimalFormat df = new DecimalFormat("#.00"); 

Note the "00", meaning exactly two decimal places.

If you use "#.##" (# means “optional” digit), it will drop trailing zeroes – ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00".

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *