English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Math round() 메서드는 지정된 값을 가장 가까운 int 또는 long 값으로 반올림하고 반환합니다.
즉,1.2반올림하여1,1.8반올림하여2。
round() 메서드의 문법은 다음과 같습니다:
Math.round(value)
주의:round()는 정적 메서드입니다. 따라서 Math 클래스 이름을 사용하여 이 메서드에 접근할 수 있습니다.
value -반올림할 수 있는 숫자
주의:이 값의 데이터 타입은 float 또는 double이어야 합니다.
파라미터가 float 일 때는 int 값을 반환합니다
파라미터가 double 일 때는 long 값을 반환합니다
round() 메서드:
소수점 이하의 값이 크거나 같을 때5,상승
1.5 => 2 1.7 => 2
소수점 이하의 값이 작을 때5,하루아래로 내림
1.3 => 1
class Main { public static void main(String[] args) { // Math.round() 메서드 //소수점 값이 큼5 double a = 1.878; System.out.println(Math.round(a)); // 2 //소수점 값이 같음5 double b = 1.5; System.out.println(Math.round(b)); // 2 //소수점 값이 작음5 double c = 1.34; System.out.println(Math.round(c)); // 1 } }
class Main { public static void main(String[] args) { // Math.round() 메서드 //소수점 값이 큼5 float a = 3.78f; System.out.println(Math.round(a)); // 4 //소수점 값이 같음5 float b = 3.5f; System.out.println(Math.round(b)); // 4 // 소수점 값이 작음5 float c = 3.44f; System.out.println(Math.round(c)); // 3 } }