// 10진수 → 2진수 / 16진수
let num = 10;
num.toString(); // "10"
num.toString(2); // "1010"
let num2 = 255;
num2.toString(16); // "ff"
let num1 = 5.1;
let num2 = 5.7;
Math.ceil(num1); // 6
Math.ceil(num2); // 6
let num1 = 5.1;
let num2 = 5.7;
Math.floor(num1); // 5
Math.floor(num2); // 5
let num1 = 5.1;
let num2 = 5.7;
Math.round(num1); // 5
Math.round(num2); // 6
// 요구사항 : 소수점 둘째자리 까지 표현 (셋째 자리에서 반올림)
let userRate = 30.1234;
userRate * 100 // 3012.34
Math.floor(userRate * 100) // 3012
Math.floor(userRate * 100) / 100 // 30.12
→ 📍 toFixed() 사용
let userRate = 30.1234;
userRate.toFixed(2); // "30.12"
userRate.toFixed(0); // "30"
userRate.toFixed(6); // "30.123400"
→ 📍 Number() 사용
let userRate = 30.1234;
Number(userRate.toFixed(2)); // 30.12
let x = Number ('x'); // NaN
x == NaN // false
x === NaN // false
x == NaN // false
→ 📍 isNaN 사용
isNaN(x) // true
isNaN(3) // false
let margin = '10px';
parseInt(margin); // 10
Number(margin); // NaN
let redColor = 'f3';
parseInt(redColor); // NaN
let redColor = 'f3';
parseInt(redColor, 16); // 243
let padding = '18.5%';
parseInt(padding); // 18
parseFloat(padding); // 18.5
// 1 ~ 100 사이 임의의 숫자를 뽑고 싶다면?
Math.floor(Math.random()*100)+1
💡 실행 시킬때 마다 다른 값 나옴
❓ 마지막에 +1을 하는 이유
→ random 값이 내림 후 0이 될 수 있기 때문에 최소값 1을 더해줌
Math.max(1, 4, -1, 5, 10, 9, 5.54); // 10
Math.min(1, 4, -1, 5, 10, 9, 5.54); // -1
absolute
의 약어Math.abs(-1); // 1
power
의 약어Math.pow(2, 10); // 1024 (2의 10승)
square root
의 약어Math.sqrt(16); // 4 (루트 16)