본문 바로가기

프론트엔드 공부/javscript

number - toFixed,parseInt,parseFloat,Math

//toFixed(2) 인수로 소수점 몇번째까지 고정할지 정함, 단 type이 string으로 변환됨
const pi = 3.141592535
console.log(pi);
const str = pi.toFixed(2);
console.log(str);//3.14
console.log(typeof str); //string

//숫자와 관련된 대표적인 전역함수parseInt, parseFloat
const integer = parseInt(str);
const float = parseFloat(str);
console.log(integer,float);  //3 3.14
console.log(typeof integer, typeof float);//number number

//내장객체 Math
//Math.abs(-x) //x //절대갇ㅄ
//Math min(a,b) //최소값, max도 동일
//Math.ceil(소수점) //올림 floor은 내림 round는 반올림
//Math.random //랜덤숫자
console.log(Math.floor(Math.random()*10)); //random한 숫자 구할때 많이 쓰는 유형