본문 바로가기

JS/TypeScript

타입스크립트 튜토리얼 Number

자바스크립트같은 타입스크립트는 숫자 값을 Number 객체로 지원한다. number객체는 숫자 리터럴을 number클래스의 인스턴스로 변환한다. number클래스는 wrapper로 동작하고 객체처럼 숫자 리터럴을 다룰 수 있다.


문법

var var_name = new Number(value)

숫자가 아닌 인수가 인수로써 Number의 생성자에 전달되는 경우, NaN을 반환한다.


다음 표는 Number객체의 속성 집합 리스트 테이블이다.

S.No.Property & Description
1.

MAX_VALUE

The largest possible value a number in JavaScript can have 1.7976931348623157E+308.

2.

MIN_VALUE

The smallest possible value a number in JavaScript can have 5E-324.

3.

NaN

Equal to a value that is not a number.

4.

NEGATIVE_INFINITY

A value that is less than MIN_VALUE.

5.

POSITIVE_INFINITY

A value that is greater than MAX_VALUE.

6.

prototype

A static property of the Number object. Use the prototype property to assign new properties and methods to the Number object in the current document.

7.

constructor

Returns the function that created this object's instance. By default, this is the Number object.

console.log("TypeScript Number Properties: "); 
console.log("Maximum value that a number variable can hold: " + Number.MAX_VALUE); 
console.log("The least value that a number variable can hold: " + Number.MIN_VALUE); 
console.log("Value of Negative Infinity: " + Number.NEGATIVE_INFINITY); 
console.log("Value of Negative Infinity:" + Number.POSITIVE_INFINITY);

출력

TypeScript Number Properties:  
Maximum value that a number variable can hold: 1.7976931348623157e+308 
The least value that a number variable can hold: 5e-324 
Value of Negative Infinity: -Infinity 
Value of Negative Infinity:Infinity


prototype 예

function employee(id:number,name:string) { 
   this.id = id 
   this.name = name 
} 

var emp = new employee(123,"Smith") 
employee.prototype.email = "smith@abc.com" 

console.log("Employee 's Id: "+emp.id) 
console.log("Employee's name: "+emp.name) 
console.log("Employee's Email ID: "+emp.email)

컴파일된 자바스크립트 코드

function employee(id, name) {
   this.id = id;
   this.name = name;
}

var emp = new employee(123, "Smith");
employee.prototype.email = "smith@abc.com";

console.log("Employee 's Id: " + emp.id);
console.log("Employee's name: " + emp.name);
console.log("Employee's Email ID: " + emp.email);

출력

Employee’s Id: 123 
Emaployee’s name: Smith 
Employee’s Email ID: smith@abc.com


Number 메서드

Number 객체는 오직 모든 객체의 정의의 부분인 기본 메서드들만 포함한다. 흔히 사용되는 메서드들은 아래와 같다.

S.No.Methods & Description
1.toExponential()

Forces a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation.

2.toFixed()

Formats a number with a specific number of digits to the right of the decimal.

3.toLocaleString()

Returns a string value version of the current number in a format that may vary according to a browser's local settings.

4.toPrecision()

Defines how many total digits (including digits to the left and right of the decimal) to display of a number. A negative precision will throw an error.

5.toString()

Returns the string representation of the number's value. The function is passed the radix, an integer between 2 and 36 specifying the base to use for representing numeric values.

6.valueOf()

Returns the number's primitive value.


https://www.tutorialspoint.com/typescript/typescript_numbers.htm


'JS > TypeScript' 카테고리의 다른 글

타입스크립트 substr() substring()  (0) 2018.12.21
타입스크립트 튜토리얼 String  (0) 2018.12.21
타입스크립트 함수  (0) 2018.12.15
타입스크립트 튜토리얼 루프  (0) 2018.12.15
타입스크립트 튜토리얼 연산자  (0) 2018.12.15