You could do something like this:
CopyDouble.valueOf(new DecimalFormat("#.##").format(
centimeters))); // 2 decimal-places
If you really wanted Math.round:
Copy(double)Math.round(centimeters * 100) / 100 // 2 decimal-places
You can have 3 decimal places by using 1000, 4 by using 10000 etc. I personally like the first option more.
Videos
You could do something like this:
CopyDouble.valueOf(new DecimalFormat("#.##").format(
centimeters))); // 2 decimal-places
If you really wanted Math.round:
Copy(double)Math.round(centimeters * 100) / 100 // 2 decimal-places
You can have 3 decimal places by using 1000, 4 by using 10000 etc. I personally like the first option more.
In order to use the Math.round method you need to change only one line in your code:
Copydouble inches = Math.round(centimeters / 2.54);
If you want to keep 2 decimal digits you can use this:
Copydouble inches = Math.round( (centimeters / 2.54) * 100.0 ) / 100.0;
By the way I suggest you a better way to deal with these problems without rounding.
Your problem is only about displaying, so you don't need to change the model of the data, you can just change its display. To print the numbers in the format you need, you can let all your logic code like this and print the result in the following way:
Add this import at the beginning of your code:
Copyimport java.text.DecimalFormat;Print the output in this way:
CopyDecimalFormat df = new DecimalFormat("#.##"); System.out.println(df.format(inches) + " Inch Is " + df.format(centimeters) + " centimeters");
The string "#.##" is the way your number will be displayed (in this example with 2 decimal digits).