Today I Learned
수학에서 자주 사용하는 절대값, 반올림, 내림, 제곱근 등을 쉽게 사용할 수 있도록 메소드의 형태로 제공하는 클래스. Math 클래스의 다양한 메소드들은 전부 static으로 구현 되어 있어 따로 객체를 생성하지 않고도 사용 가능하다.
abs(a) : 인자로 넘긴 데이터의 절댓값을 반환
random() : 0.0~1.0 사이의 임의의 double형 데이터 생성하여 반환
max(a, b) : 전달된 데이터 중 더 큰 수를 반환
static int max(int a, int b)
static long max(long a, long b)
static double max(double a, double b)
static float max(float a, float b)
static int min(int a, int b)
static long min(long a, long b)
static double min(double a, double b)
static float min(float a, float b)
import java.lang.Math;
// round 이용해 소수점 n번째 자리 표현
public class roundPrac {
public static void main(String[] args) {
float n = 123.123456789f;
System.out.println("round() : " + Math.round(n));
System.out.println("round() 이용해 소수점 두번째 자리수까지 표현 : " + Math.round(n*100.0)/100.0);
System.out.println("round() 이용해 소수점 번째 자리수까지 표현: " + Math.round(n*10000.0) /10000.0);
}
}
pow(a, b) : a를 b 제곱 한 값
sqrt(a) : a의 제곱근 값