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
COPY TO CLIPBOARD
1 decimal place
double number = 123.456789;
double rounded = Math.round(number * 10.0) / 10.0;
System.out.println(rounded); // 123.5
COPY TO CLIPBOARD
2 decimal places
double number = 123.456789;
double rounded = Math.round(number * 100.0) / 100.0;
System.out.println(rounded); // 123.46
COPY TO CLIPBOARD
3 decimal places
double number = 123.456789;
double rounded = Math.round(number * 1000.0) / 1000.0;
System.out.println(rounded); // 123.457
COPY TO CLIPBOARD
4 decimal places
double number = 123.456789;
double rounded = Math.round(number * 10000.0) / 10000.0;
System.out.println(rounded); // 123.4568
COPY TO CLIPBOARD
5 decimal places
double number = 123.456789;
double rounded = Math.round(number * 100000.0) / 100000.0;
System.out.println(rounded); // 123.45679
COPY TO CLIPBOARD