반올림 : Math.round(값)
올림 : Math.ceil(값)
내림 : Math.floor(값)
소수점 반올림/올림/내림을 하고 싶은 경우, 원하는 자릿수의 배수만큼 곱한 값을 Math 처리한 후 곱한만큼 나눠주면 된다.
public static void main(String[] args) {
double a = 3.14159265358979;
System.out.println("첫째 자리 반 올림 : " + Math.round(a));
System.out.println("둘째 자리 반 올림 : " + Math.round(a * 10) / 10.0);
System.out.println("첫째 자리 올림 : " + Math.ceil(a));
System.out.println("셋째 자리 올림 : " + Math.ceil(a * 100) / 100.0);
System.out.println("첫째 자리 버림 : " + Math.floor(a));
System.out.println("넷째 자리 버림 : " + Math.floor(a * 1000) / 1000.0);
}
참고: 소수점 반올림/올림/내림