JavaScript - Number/Math Methods

LANA·2020년 4월 6일

JavaScript

목록 보기
5/21
post-thumbnail

Number.isInteger(value)
arguments: 정수인지, 아닌지 여부를 검사할 값
return value: 정수를 판단한 결과 (Boolean)

Number.isInteger(0);         // true
Number.isInteger(1);         // true
Number.isInteger(-100000);   // true

Number.isInteger(0.1);       // false
Number.isInteger(Math.PI);   // false

Number.isInteger(Infinity);  // false
Number.isInteger(-Infinity); // false
Number.isInteger("10");      // false
Number.isInteger(true);      // false
Number.isInteger(false);     // false
Number.isInteger([1]);       // false

parseInt(value) / parseFloat(value)
arguments: 형변환(type casting)하기 위해 파싱될 값
return value: 정수 또는 소숫점 숫자

parseInt("15");     // 15
parseInt("15.123"); // 15
parseInt("15*3");   // 15
parseInt("-15");    // -15
parseInt("Hello")   // NaN
parseFloat("3.14"); // 3.14

parseInt(value, radix)
parseInt는 진법으로 변환할 때도 사용함
radix가 필요없는 10진법 변환일 경우에도, 명시적으로 10을 넣어주는 것을 권장

parseInt(" 0xF", 16); // 15
parseInt(" F", 16);   // 15
parseInt("17", 8);    // 15
parseInt(15.99, 10);  // 15

num.toFixed([digits])
arguments: 소숫점 뒤에 나타낼 자릿수 (optional, 기본값은 0)
return value: 숫자를 나타내는 문자열

var numObj = 12345.6789;
numObj.toFixed();   // '12346': 반올림하며, 소수 부분을 남기지 않습니다.
numObj.toFixed(1);  // '12345.7': 반올림합니다.
numObj.toFixed(6);  // '12345.678900': 빈 공간을 0으로 채웁니다.

Math.min([value1[, value2[, ...]]])
/ Math.max([value1[, value2[, ...]]])

arguments: 숫자
return value: 주어진 숫자 중 가장 작은/큰 값

console.log(Math.min(2, 3, 1)); // 1
console.log(Math.min(-2, -3, -1)); // -3
console.log(Math.max(1, 3, 2)); // 3
console.log(Math.max('hello', 'world')); // NaN

Math.floor(x) / Math.round(x)
arguments: 숫자
return value: 주어진 숫자의 내림/반올림 값

console.log(Math.floor( 45.95)); //  45
console.log(Math.floor(-45.95)); // -46
console.log(Math.round( 20.5 )); //  21
console.log(Math.round(-20.5 )); // -20

Math.random()
arguments: 없음
return value: 0과 1 사이의 난수를 반환

console.log(Math.random()); // 0.19332486207208088

LEARN YOURSELF
abs
sqrt / pow
Math.random()을 이용해, 특정 범위의 랜덤한 정수 리턴하기 getRandomInt()
Math.random()을 이용해, 일정한 길이의 임의의 글자 리턴하기 getGUID()

profile
Let's code like chord !

0개의 댓글