TypeScript – Numbers


TypeScript – Numbers


”;


TypeScript like JavaScript supports numeric values as Number objects. A number object converts numeric literal to an instance of the number class. The Number class acts as a wrapper and enables manipulation of numeric literals as they were objects.

TypeScript number type represents the numeric values. All the numbers are represented as the floating point values. TypeScript also supports the binary, octal and hexadecimal numbers introduced in ECMAScript 2015.

In TypeScript, we can create a number primitive as well as Number object.

Syntax

We can declare a variable of number type using colon (:) after the variable name followed by number −

let varName: number = value;

In the above syntax, we declared a variable named varName of number type. Here value is the any numeric value such as decimal, binary, octal or hexadecimal numbers.

We can create the Number object. To create a Number object we can use the Number() constructor as follows −

var var_name = new Number(value)

In case a non-numeric argument is passed as an argument to the Number’s constructor, it returns NaN (Not–a–Number)

The type ”Number” is a wrapper object but type ”number” is a primitive. Prefer using ”number” when possible. Type ”Number” is not assignable to type ”number”.

Creating Number Types

In the below example, we created a variable count of number type. We assigned 10 to the count.

let count: number = 10;
console.log(count);

On compiling, it will generate the following JavaScript code.

let count = 10;
console.log(count);

The output is as follows −

10

We can also assign float, binary, octal and hexadecimal values to a variable of number type. Look at the below TypeScript code snippet –

let decNum: number = 10.6; // floating point number
let binNum: number = 0b101001; // binary number
let octNum: number = 0o45; // octal number
let hexNum: number = 0x80fd; // hexadecimal number

Creating Number Object

const count = new Number(10);
console.log(count);
console.log(typeof count);

On compiling, it will generate the same JavaScript code.

The output of the above example code is as follows –

[Number: 10]
Object

Number Properties

The following table lists a set of properties of the Number object −

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.

Example

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);

On compiling, it will generate the same code in JavaScript.

Its output is as follows −

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

Example: NaN

var month = 0 
if( month<=0 || month >12) { 
   month = Number.NaN 
   console.log("Month is "+ month) 
} else { 
   console.log("Value Accepted..") 
}

On compiling, it will generate the same code in JavaScript.

Its output is as follows −

Month is NaN

Example: prototype

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

var emp = new employee(123,"Smith") 
employee.prototype.email = "[email protected]" 

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

On compiling, it will generate the following JavaScript code −

//Generated by typescript 1.8.10
function employee(id, name) {
   this.id = id;
   this.name = name;
}

var emp = new employee(123, "Smith");
employee.prototype.email = "[email protected]";

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

Its output is as follows −

Employee''s Id: 123 
Emaployee''s name: Smith 
Employee''s Email ID: [email protected]

Number Methods

The Number object contains only the default methods that are a part of every object”s definition. Some of the commonly used methods are listed below −

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.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *