Number.prototype 메서드
Object.prototype.toString() - 숫자를 문자열로 반환한다.
function Dog(name) {
this.name = name;
}
const dog1 = new Dog('Gabby');
Dog.prototype.toString = function dogToString() {
return `${this.name}`;
};
console.log(dog1.toString());
// Expected output: "Gabby"
Number.prototype.toFixed() - 숫자를 고정 소수점 표기법으로 표시한다.
const numObj = 12345.6789;
numObj.toFixed(); // '12346'; rounding, no fractional part
numObj.toFixed(1); // '12345.7'; it rounds up
numObj.toFixed(6); // '12345.678900'; additional zeros
(1.23e+20).toFixed(2); // '123000000000000000000.00'
(1.23e-10).toFixed(2); // '0.00'
2.34.toFixed(1); // '2.3'
2.35.toFixed(1); // '2.4'; it rounds up
2.55.toFixed(1); // '2.5'
// it rounds down as it can't be represented exactly by a float and the
// closest representable float is lower
2.449999999999999999.toFixed(1); // '2.5'
// it rounds up as it's less than NUMBER.EPSILON away from 2.45.
// This literal actually encodes the same number value as 2.45
(6.02 * 10 ** 23).toFixed(50); // 6.019999999999999e+23; large numbers still use exponential notation
Number.prototype.toPrecision() - 지정된 길이로 작성된 숫자를 문자열로 반환한다.
var numObj = 5.123456;
console.log(numObj.toPrecision()); // logs '5.123456'
console.log(numObj.toPrecision(5)); // logs '5.1235'
console.log(numObj.toPrecision(2)); // logs '5.1'
console.log(numObj.toPrecision(1)); // logs '5'
numObj = 0.000123
console.log(numObj.toPrecision()); // logs '0.000123'
console.log(numObj.toPrecision(5)); // logs '0.00012300'
console.log(numObj.toPrecision(2)); // logs '0.00012'
console.log(numObj.toPrecision(1)); // logs '0.0001'
// 일부 상황에서는 지수 표기법이 반환 될 수 있습니다
console.log((1234.5).toPrecision(2)); // logs '1.2e+3'
Object.prototype.valueOf() - number 타입으로 반환한다.
function MyNumberType(n) {
this.number = n;
}
MyNumberType.prototype.valueOf = function() {
return this.number;
};
const object1 = new MyNumberType(4);
console.log(object1 + 3);
// Expected output: 7
Number 메서드
Number() - 변수를 숫자로 반환한다.
Number('123'); // 숫자 123을 반환
Number('123') === 123; // 참
Number('unicorn'); // NaN
Number(undefined); // NaN
parseInt() - 문자열에 포함된 숫자 부분을 정수 형태로 반환한다.
parseInt("-10"); // -10
parseInt("-10.33"); // -10
parseInt("10"); // 10
parseInt("10.33"); // 10
parseInt("10 20 30"); // 10
parseInt("10 years"); // 10
parseInt("years 10"); // NaN
parseFloat() - 문자열에 포함된 숫자 부분을 실수 형태로 반환한다.
// 아래 예제는 모두 3.14를 반환한다.
parseFloat(3.14);
parseFloat('3.14');
parseFloat(' 3.14 ');
parseFloat('314e-2');
parseFloat('0.0314E+2');
parseFloat('3.14와 숫자가 아닌 문자들');
parseFloat({ toString: function() { return "3.14" } });
isNaN() - 전달된 값이 NaN인지 아닌지 검사한다.
isNaN(NaN); // 참
isNaN(undefined); // 참
isNaN({}); // 참
isNaN(true); // 거짓
isNaN(null); // 거짓
isNaN(37); // 거짓
// 문자열
isNaN("37"); // 거짓: "37"은 NaN이 아닌 숫자 37로 변환됩니다
isNaN("37.37"); // 거짓: "37.37"은 NaN이 아닌 숫자 37.37로 변환됩니다
isNaN("123ABC"); // 참: parseInt("123ABC")는 123이지만 Number("123ABC")는 NaN입니다
isNaN(""); // 거짓: 빈 문자열은 NaN이 아닌 0으로 변환됩니다
isNaN(" "); // 거짓: 공백이 있는 문자열은 NaN이 아닌 0으로 변환됩니다
// dates
isNaN(new Date()); // 거짓
isNaN(new Date().toString()); // 참
// 이것이 허위 양성이고 isNaN이 완전히 신뢰할 수 없는 이유이다.
isNaN("blabla") // 참: "blabla"는 숫자로 변환됩니다.
// 이것을 숫자롯 parsing 하는 것을 실패하고 NaN을 반환한다.
Math 메서드
Math.round() - 소수점 첫 번째 자리에서 반올림한다.
Math.round( 20.49); // 20
Math.round( 20.5 ); // 21
Math.round( 42 ); // 42
Math.round(-20.5 ); // -20
Math.round(-20.51); // -21
Math.ceil() - 가장 가까운 정수로 올림한다.
Math.ceil(.95); // 1
Math.ceil(4); // 4
Math.ceil(7.004); // 8
Math.ceil(-0.95); // -0
Math.ceil(-4); // -4
Math.ceil(-7.004); // -7
Math.floor() - 가장 가까운 정수로 내림한다.
Math.floor( 45.95); // 45
Math.floor( 45.05); // 45
Math.floor( 4 ); // 4
Math.floor(-45.05); // -46
Math.floor(-45.95); // -46
Math.trunc() - 소수점 부분을 버리고 정수만을 반환한다.
Math.trunc(13.37); // 13
Math.trunc(42.84); // 42
Math.trunc(0.123); // 0
Math.trunc(-0.123); // -0
Math.trunc('-1.123'); // -1
Math.trunc(NaN); // NaN
Math.trunc('foo'); // NaN
Math.trunc(); // NaN
Math.pow(x,y) - x의 y승을 반환한다.
// 간단한 예
Math.pow(7, 2); // 49
Math.pow(7, 3); // 343
Math.pow(2, 10); // 1024
// 분수 지수
Math.pow(4, 0.5); // 2 (4의 제곱근)
Math.pow(8, 1/3); // 2 (8의 세제곱근)
Math.pow(2, 0.5); // 1.4142135623730951 (2의 제곱근)
Math.pow(2, 1/3); // 1.2599210498948732 (2의 세제곱근)
// 양의 지수
Math.pow(7, -2); // 0.02040816326530612 (1/49)
Math.pow(8, -1/3); // 0.5
// 양의 밑
Math.pow(-7, 2); // 49 (제곱의 결과값은 양수입니다.)
Math.pow(-7, 3); // -343 (세제곱은 음수가 될 수 있습니다.)
Math.pow(-7, 0.5); // NaN (음수는 실제 제곱근을 가지지 않습니다.)
// "짝수"와 "홀수" 근이 서로 가깝게 놓여 있고
// 부동소수점 정밀도의 한계로 인해,
// 밑이 음수이며 지수가 분수라면 언제나 NaN을 반환합니다.
Math.pow(-7, 1/3); // NaN
Math.sqrt() - x의 제곱근을 반환한다.
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(-1); // NaN
Math.abs() - 절댓값(양수)을 반환한다.
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1,2]); // NaN
Math.abs({}); // NaN
Math.abs('string'); // NaN
Math.abs(); // NaN
Math.min(x,y,...) - 인수 목록 중 가장 작은 수를 반환한다.
console.log(Math.min(1, 3, 2));
// Expected output: 1
console.log(Math.min(-1, -3, -2));
// Expected output: -3
const array1 = [1, 3, 2];
console.log(Math.min(...array1));
// Expected output: 1
Math.max(x,y,...) - 인수 목록 중 가장 큰 수를 반환한다.
console.log(Math.max(1, 3, 2));
// Expected output: 3
console.log(Math.max(-1, -3, -2));
// Expected output: -1
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
// Expected output: 3
Math.random() - 0보다 크거나 같고 1보다 작은 랜덤 숫자를 반환한다.
const num = Math.random();
console.log(num);
// Expected output: 0.48054336144077503