Convert double to 2 decimal places?
Am trying to calculate the volume using 2 variables both of which are in cm and the requirement is to display in inches.
Both length and diam can only be entered in as whole numbers.
length = cl.getUserLength();
diam = cd.getUserDiameter();
volume = ((diam / 2) * length) / 2.54;
Thats my code so far. (diam / 2) is to get the radius and / 2.54 is to get it to inches. However for example if both measurements are say 10, i get a output of 19.68503937007874
I want it down to 2 decimal places. I have checked alot online and references to BigDecimal and DecimalFormat, however tutorials are very unclear on how to implement.
All help will be appreciated.
Re: Convert double to 2 decimal places?
If you are going to be displaying the number on the console / standard output stream, then using System.out.printf should do the trick. Since a double is a floating point number, you can use something like %.2f as the format specifier to format your String.
For more on how to use this, please look here: formatting. Note that System.out.format works the same way as System.out.printf.