EnJinnier
[JS] 수학 method (Number, Math) 본문
toString() :문자열로 바꿔주기
:문자열로 바꿔주는 메소드.
+ 10진수를 2진수나 16진수로 만들어줄 수도 있다.
let num = 10;
//문자열로 바꾸기
num.toString(); //"10"
//2진수로 바꾸기
num.toString(2); //"1010"
//16진수로 바꾸기
let num2=255;
num2.toString(16); //"ff"
parseInt() : 문자열을 숫자로
2진수, 16진수로도 변경 가능
let margin = '10px';
let redColor='f3';
parseInt(margin); //10
parseInt(redColor); //NaN
parseFloat() : 문자열을 소수로
let padding = '18.5%';
parseFloat(padding); //18.5
Math.ceil() : 올림
//Math.ceil() : 올림
let num1 = 5.1;
Math.ceil(num1); //6
Math.floor(): 내림
//Math.floor(): 내림
let num2 = 5.7;
Math.floor(num2); //5
Math.round() : 반올림
let num1 = 5.1;
let num2 = 5.7;
Math.round(num1) //5
Math.round(num2) //6
toFixed() : 소수점 자리수 설정
let userRate= 30.1234;
userRate.toFixed(2); //"30.12"
Math.random() : 0~1 사이 무작위 숫자 생성
만약 1~100 사이 임의의 '정수' 숫자를 뽑고 싶다면?
//소수점도 모두 고려되는 랜덤뽑기
Math.random();
//정수뽑기
Math.floor(Math.random()*100)+1;
Math.max() / Math.min() : 최대/최소값 반환
Math.max(1,4,-3,10); //10
Math.abs() : 절대값
Math.abs(-1) //1
Math.pow(n,m) : 제곱
Math.pow(2,5); //32
Math.wqrt() : 제곱근
Math.sqrt(16); //4
'웹 개발 > JavaScript' 카테고리의 다른 글
[리액트] Get API 호출했을때 Undefined 값이 나옴 (0) | 2024.08.22 |
---|---|
[JS] 배열 메소드(Array methods) (0) | 2024.04.06 |
[JS] 객체 메소드(Object methods) (0) | 2024.04.05 |