Math
Math
1. Math
Math는 수학적인 상수와 함수를 위한 속성과 메서드를 가진 내장 객체
Math.ceil() - JavaScript | MDN
- Math.ceil(x)
주어진 숫자보다 크거나 같은 숫자 중 가장 작은 숫자를 integer로 반환.
- 매개변수 : 숫자
- 올림
- 정수로 반환
- 소수점 자릿수에 상관없이 소수 자리의 값이 있다면 올리거나 내린다.
let num1 = 5.1;
let num2 = 5.7;
Math.ceil(num1);
console.log(Math.ceil(num1));
Math.ceil(num2);
console.log(Math.ceil(num2));
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
6
6
[Done] exited with code=0 in 0.049 seconds
- Math.floor(x)
Math.floor() 함수는 **Math.floor()**함수는 주어진 숫자와 같거나 작은 정수 중에서 가장 큰 수를 반환
Math.floor() - JavaScript | MDN
- 매개변수 : 숫자
- 내림
- 정수로 반환
- 소수점 자릿수에 상관없이 소수 자리의 값이 있다면 올리거나 내린다.
let num1 = 5.1;
let num2 = 5.7;
Math.floor(num1);
console.log(Math.floor(num1));
Math.floor(num2);
console.log(Math.floor(num2));
=>결과값
[Running] node "/workspaces/codespaces-blank/index.js"
5
5
[Done] exited with code=0 in 1.421 seconds- Math.round(x)
Math.round() 함수는 입력값을 반올림한 수와 가장 가까운 정수 값을 반환
Math.round() - JavaScript | MDN
- 매개변수 : 숫자
- 반올림
- 정수로 반환
let num1 = 5.1;
let num2 = 5.7;
console.log(Math.round(num1));
console.log(Math.round(num2));
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
5
6
[Done] exited with code=0 in 0.061 seconds- toFixed()
숫자를 고정 소수점 표기법(fixed-point notation)으로 표시
Number.prototype.toFixed() - JavaScript | MDN
- 매개변수 : 숫자
- 소수점 셋째 자리에서 반올림을 원할 경우
toFixed(n)메소드를 사용함
let myNumber = 58.112512512421424;
console.log(Number(myNumber.toFixed(2)));
=>결과값
[Running] node "/workspaces/codespaces-blank/index.js"
58.11
[Done] exited with code=0 in 0.049 seconds- Math.random(x)
Math.random() 함수는 0 이상 1 미만의 구간에서 근사적으로 균일한(approximately uniform) 부동소숫점 의사난수를 반환하며, 이 값은 사용자가 원하는 범위로 변형할 수 있다. 난수 생성 알고리즘에 사용되는 초기값은 구현체가 선택하며, 사용자가 선택하거나 초기화할 수 없다.
*참고:
Math.random()은 암호학적으로 안전한 난수를 제공하지 않으므로, 보안과 관련된 어떤 것에도 이 함수를 사용해서는 안 된다. 그 대신 Web Crypto API의 window.crypto.getRandomValues() 메소드를 이용하여야 한다.
Math.random() - JavaScript | MDN
- 매개변수 : 숫자
- 0 이상 1 미만의 부동소숫점 의사 난수.
const num =Math.random()
console.log('0~10 사이의 랜덤한 숫자:',num*10)
console.log('0~50 사이의 랜덤한 숫자:',num*50)
=>결과값
[Running] node "/workspaces/codespaces-blank/index.js"
0~10 사이의 랜덤한 숫자: 2.2020552161617157
0~50 사이의 랜덤한 숫자: 11.010276080808579
[Done] exited with code=0 in 0.048 seconds- Math.max(x)
Math.max() 함수는 입력값으로 받은 0개 이상의 숫자 중 가장 큰 숫자를 반환
- 매개변수 : 값1, 값2, …
- 입력된 숫자 중 가장 큰 숫자를 반환합니다. 만약 인수 중 하나라도 숫자로 변환하지 못한다면 NaN로 반환
console.log(Math.max(1, 4, 10, -1, 2.24))
=>결과값
[Running] node "/workspaces/codespaces-blank/index.js"
10
[Done] exited with code=0 in 0.05 seconds- Math.min(x)
Math.min() 함수는 주어진 숫자들 중 가장 작은 값을 반환
- 매개변수 : 값1, 값2, …
- 입력된 숫자 중 가장 작은 숫자를 반환합니다. 만약 인수 중 하나라도 숫자로 변환하지 못한다면 NaN로 반환
console.log(Math.min(1, 4, 10, -1, 2.24))
=>결과값
[Running] node "/workspaces/codespaces-blank/index.js"
-1
[Done] exited with code=0 in 0.055 seconds- Math.abs(x)
Math.abs() 함수는 주어진 숫자의 절대값을 반환합니다. x가 양수이거나 0이라면 x를 리턴하고, x가 음수라면 x의 반대값, 즉 양수를 반환
- 매개변수 : 숫자
- 주어진 숫자의 절대값.
- 빈 객체, 하나 이상의 요소를 가진 배열, 숫자가 아닌 문자열,undefined나 빈 매개변수를 받으면 NaN을 반환합니다.null, 빈 문자열이나 빈 배열을 제공하면 0을 반환
function difference(a, b) {
return Math.abs(a - b);
}
console.log(difference(3, 5));
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2
[Done] exited with code=0 in 0.196 seconds- Math.pow(base,exponent)
Math.pow()함수는base^exponent처럼 base에 exponent를 제곱한 값을 반환
- 매개변수 : base(밑 값) , exponent(지수)
- 주어진 밑 값을 주어진 지수 값으로 거듭제곱한 숫자 값
console.log(Math.pow(7, 3));
console.log(Math.pow(7, -2));
console.log(Math.pow(-7, 0.5));
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
343
0.020408163265306124
NaN
[Done] exited with code=0 in 0.071 seconds
- Math.sqrt(x)
Math.sqrt()함수는 숫자의 제곱근을 반환
Math.sqrt() - JavaScript | MDN
- 매개변수 : 숫자
- 주어진 숫자에 루트(√ )를 씌웁니다. 만약 숫자가 음수이면 NaN를 반환
console.log(Math.sqrt(16));
console.log(Math.sqrt(2));
console.log(Math.sqrt(-1));
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
4
1.4142135623730951
NaN
[Done] exited with code=0 in 0.048 seconds