JavaScript 메서드로 데이터 다루기(4. 숫자 다루기)

Park, Jinyong·2020년 4월 9일
0

Code States - Pre Course

목록 보기
6/11

Number 메서드

Number.insInteger

  • arguments: 정수 여부를 검사
  • return value: boolean
Number.isInteger(0); // true
Number.isInteger(Math.PI); // false
Number.isInteger([1]); // false
Number.isInteger('100'); // false

parseInt / parseFloat

  • arguments: 형변환하기 위해 parsing될 값, (진법 = 10)
  • return value: 정수 또는 실수
parseInt('1'); // 1
parseInt('1.1'); // 1
parseInt('string'); // NaN
parseFloat('3.14'); // 3.14

parseInt("0xF", 16); // 15
parseInt("1", 10); // 1
parseInt("10", 8); // 10
parseInt("111", 2); // 7

toFixed

  • arguments: (소숫점 뒤에 나타낼 자릿수 = 0)
  • return value: 숫자를 나타내는 문자열
const realNumber = 12345.6789;
realNumber.toFixed(); // '12346'
realNumber.toFixed(1); // '12345.7'
realNumber.toFixed(6);//  '12345.678900'

Math 메서드

Math.max / Math.min

  • arguments: 숫자, 숫자, 숫자, ...
  • return value: 가장 큰 / 작은 값
Math.min(5, 100, 25); // 5
Math.max(5, 100, 25); // 100
Math.max('hello', 'world'); // NaN

Math.floor / Math.round

  • arguments: 숫자
  • return value: 주어진 숫자를 내림 / 반올림한 값
Math.floor(456.789); // 456
Math.floor(-456.789); // -457
Math.round(234.567); // 235
Math.round(-234.567); // -235

Math.random

  • arguments: 없음
  • return value: 0과 1 미만의 난수
Math.random(); // 0.6980786592490051

Math.abs

  • arguments: 숫자
  • return value: 주어진 숫자의 절대값
Math.abs(-3); // 3
Math.abs('-3'); // 3
Math.abs(null); // 0
Math.abs([2]); // 2

Math.abs([1,2]); // NaN
Math.abs({}); // NaN
Math.abs('string'); // NaN
Math.abs(); // NaN

Math.sqrt

  • arguments: 숫자
  • return value: 제곱근
Math.sqrt(256); // 16 
Math.sqrt(2); // 1.414213562373095
Math.sqrt(-1); // NaN

Math.pow

  • arguments: 밑 (base), 지수 (exponent)
  • return value: base^exponent
Math.pow(5, 2); // 25
Math.pow(5, 3); // 125
Math.pow(-5, 3); // -125
Math.pow(2, 0.5); // 1.4142135623730951
Math.pow(8, -1/3); // 0.5

0개의 댓글