본문 바로가기

Programing/JavaScript

[JavaScript] Number/Math Methods // Number.prototype.toString() / Number.isInteger / parseInt(value) / parseFloat(value) / num.toFixed() / Math.min / Math.max / Math.floor / Math.round / Math.random() / Math.sqrt() / Math.pow

Number

MDN문서

 

숫자형 문자열에서 숫자로 변환

 

1
2
3
4
5
6
7
8
9
10
Number('123')     // 123
Number('12.3')    // 12.3
Number('123e-1')  // 12.3
Number('')        // 0
Number(null)      // 0 
Number('0x11')    // 17
Number('0b11')    // 3
Number('0o11')    // 9
Number('foo')     // NaN
Number('100a')    // NaN
cs

 

 

 

 

Number.prototype.toString()

MDN문서

number를 string으로 바꿔주는 메소드
 
1
2
3
4
5
6
7
8
9
10
11
12
13
var count = 10;
 
console.log(count.toString());    // displays '10'
console.log((17).toString());     // displays '17'
console.log((17.2).toString());   // displays '17.2'
 
var x = 6;
 
console.log(x.toString(2));       // displays '110'
console.log((254).toString(16));  // displays 'fe'
 
console.log((-10).toString(2));   // displays '-1010'
console.log((-0xff).toString(2)); // displays '-11111111'
cs

 

 

 

Number.isInteger(value)

· arguments: 정수인지, 아닌지 여부를 검사할 값

· return value: 정수를 판단한 결과 (Boolean)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
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
cs

 

 

 

parseInt(value) / parseFloat(value)

· arguments: 형변환(type casting)하기 위해 파싱될 값

· return value: 정수 또는 소숫점 숫자

 

1
2
3
4
5
6
parseInt("15");     // 15
parseInt("15.123"); // 15
parseInt("15*3");   // 15
parseInt("-15");    // -15
parseInt("Hello")   // NaN
parseFloat("3.14"); // 3.14
cs

 

 

 

num.toFixed([digits])

· arguments: 소숫점 뒤에 나타낼 자릿수 (optional, 기본값은 0)

· return value: 숫자를 나타내는 문자열

 

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

 

 

 

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

· arguments: 숫자

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

 

1
2
3
4
console.log(Math.min(231)); // 1
console.log(Math.min(-2-3-1)); // -3
console.log(Math.max(132)); // 3
console.log(Math.max('hello''world')); // NaN
cs

 

 

 

Math.floor(x) / Math.round(x)

· arguments: 숫자

· return value: 주어진 숫자의 내림/반올림 값

 

1
2
3
4
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
cs

 

 

 

Math.random()

· arguments: 없음

· return value: 0과 1 사이의 난수를 반환

 

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

 

 

 

Math.sqrt()

Math.sqrt()함수는 숫자의 제곱근을 반환

 

1
2
console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(25)); // 5
cs

 

 

 

Math.pow

Math.pow(base, exponent) / exponent : base의 지수

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
console.log(Math.pow(22));
// expected output: 4
 
console.log(Math.pow(73));
// expected output: 343
 
console.log(Math.pow(40.5));
// expected output: 2
 
console.log(Math.pow(7-2));
// expected output: 0.02040816326530612
//                  (1/49)
 
console.log(Math.pow(-70.5));
// expected output: NaN
 
cs