1. toString
10진수 -> 2진수/16진수
let num = 10;
num.toString();// "10"
num.toString(2); // "1010"
let num2 = 225;
num2.toString(16); //"ff"
2. Math
Math.ceil(): 올림
let num1 = 5.1;
let num2 = 5.7;
Math.ceil(num1); //6
Math.ceil(num2); //6
Math.floor(): 내림
let num1 = 5.1;
let num2 = 5.7;
Math.floor(num1); //5
Math.floor(num2); //5
Math.round(): 반올림
let num1 = 5.1;
let num2 = 5.7;
Math.round(num1); //5
Math.round(num2); //6
소수점 자릿수 (요구사항: 소수점 둘째자리 까지 표현(셋째 자리에서 반올림) and toFixed()
let userRate = 30.1234
Math.round(userRate * 100)/100 // 30.12
userRate.toFixed(2); // "30.12"
userRate.toFixed(0); // "30"
userRate.toFixed(6); // "30.123400"
- toFixed 는 문자열로 반화하기 때문에 Number 함수를 통해 바꾸어주면 된다.
parseInt()
- 문자열을 숫자로 바꾸어준다. Number 와 다른점은 문자가 혼용되어있어도 동작하게된다.
let margin = '10px';
parseInt(margin); // 10
Numper(margin); // NaN
let redColor = 'f3';
parseInt(redColor); // NaN
parseInt(redColor, 16); // 243 16진수로 바꾸어줌
parseInt('11',2) // 3 10진수로 바꾸어줌
parseFloat()
let padding = '18.5%'
parseInt(padding) // 18
parseFloat(padding) // 18.5
Math.random() : 0~1 사이 무작위 숫자 생성
- 1~100 사이 임의의 숫자를 뽑고 싶다면 ?
Math.floor(Math.random()*100)+1
Math.max() / Math.min()
Math.max(1,4,-1,5,10,9,5.54); // 10
Math.min(1,4,-1,5,10,9,5.54); // -1
Math.abs() : 절대값
Math.abs(-1) // 1
Math.pow(n,m) : 제곱
Math.pow(2,10); // 1024
Math.sqrt() : 제곱근
Math.sqrt(16) // 4