To round decimals to
N
places in Java, do this: Math.round(value * N * 10.0) / (N * 10.0)
.0 decimal places
double number = 123.456789;
int rounded = (int)Math.round(number);
System.out.println(rounded); // 123
1 decimal place
double number = 123.456789;
double rounded = Math.round(number * 10.0) / 10.0;
System.out.println(rounded); // 123.5
2 decimal places
double number = 123.456789;
double rounded = Math.round(number * 100.0) / 100.0;
System.out.println(rounded); // 123.46
3 decimal places
double number = 123.456789;
double rounded = Math.round(number * 1000.0) / 1000.0;
System.out.println(rounded); // 123.457
4 decimal places
double number = 123.456789;
double rounded = Math.round(number * 10000.0) / 10000.0;
System.out.println(rounded); // 123.4568
5 decimal places
double number = 123.456789;
double rounded = Math.round(number * 100000.0) / 100000.0;
System.out.println(rounded); // 123.45679