Number, Math
참고 강의
코딩앙마님의 유튜브 강의를 듣고 공부 내용을 정리한 것입니다.
Number
let num = 10;
num.toString(); // "10"
num.toString(2); // 2진수로 변경 "1010"
let num2 = 255;
num2.toString(16); // 16진수로 변경 "ff"
Math
let num1 = 5.1;
let num2 = 5.7;
Math.round(num1);
Math.round(num2);
let userRate = 30.1234;
Math.round(userRate * 100)/100 // 30.12
Math.toFixed(2) // "30.12"
Math.toFixed() // "30"
Number(Math.toFixed(2)); // 30.12
let x = Number('x'); // NaN
x == NaN // false
x === NaN // false
NaN == NaN // false
isNaN(x) // true
isNaN(3) // false
let margin = '10px';
parseInt(margin); // 10
Number(margin); // NaN
let redColor = 'f3';
parseInt(redColor); // NaN
parseInt(redColor, 16); // 243 (16진수로 변경)
parseInt('11', 2) // 3 (2진수로 변경)
let padding = '18.5%';
parseInt(padding); // 18
parseFloat(padding); // 18.5
// 0 ~ 1 사이의 무작위 숫자 생성
Math.random();
// 1 ~ 100 사이 임의의 숫자를 뽑고 싶다면?
Math.floor(Math.random()*100)+1
// 1. (0.6789 * 100)
// 2. 67
// 3. 67 + 1
Math.max(1, 4, -1, 5, 10, 9, 5.54); // 10
Math.min(1, 4, -1, 5, 10, 9, 5.54); // -1
Math.abs(-1) // 1
Math.pow(2, 10); // 1024
Math.sqrt(16) // 4