[JavaScript] 05. Number & Math

SSOYEONG·2022년 7월 25일
0

자바스크립트 기초

목록 보기
4/11
post-thumbnail

toString()

  • 10진수 -> 2진수/16진수
let num = 10;

num.toString(); 	// "10"
num.toString(2); 	// "1010"

let num2 = 255;
num2.toString(16);		// "ff"

Math

let num = 5.1;

Math.ceil(num);		// 6
Math.floor(num);	// 5
Math.round(num);	// 5

let userRate = 30.1234;
// 소수점 둘째자리까지 표현
userRate.toFixed(2);		// "30.12"	문자열로 반환한다
userRate.toFixed(6);		// "30.123400"

let x = Number('x');	// NaN
x == NaN	// false
x === NaN	// false
NaN === NaN	// false !
isNaN(x)	// true
isNaN(3)	// flase

let margin = '10px';
parseInt(margin);	// 10
Number(margin);		// NaN

let redColor = 'f3';
parseInt(redColor);	// NaN
parseInt(redColor, 16);	// 243. 16진수로 바꾼 후 변환

parseInt('11', 2);	// 3

let padding = '18.5%';
parseInt(padding);		// 18
parseFloat(padding);	// 18.5

Math.random();	// 0~1 무작위
Math.floor(Math.random() * 100) + 1		// 1~100 무작위

Math.max(1, 2, 3, 4);	// 4
Math.min(1, 2, 3, 4);	// 1
Math.abs(-1);			// 1
Math.pow(2, 10);		// 1024
Math.sqrt(16);			// 4
profile
Übermensch

0개의 댓글