Here are a few ways to round a double in Java to the nearest integer, tenths, hundredths, or thousandths decimal place.
double x = 0.0d; double y = 0.0d; // Double rounded to the nearest integer. y = (int) (x + 0.5); // Double rounded to the nearest tenth. y = ((int) ((x * 10) + 0.5)) / 10; // Double rounded to the nearest hundredth. y = ((int) ((x * 100) + 0.5)) / 100; // Double rounded to the nearest thousandth. y = ((int) ((x * 1000) + 0.5)) / 1000;
It worked for me with a minor modification. In order to work it properly all the divisors has to be doubles instead of integers. Adding a decimal point and a zero at the end of all these divisors (e.g., 10.0, 100.0, 1000.0) worked for me.
Thanks and keep up the good work!