TypeScript Numbers

TypeScript is like JavaScript which supports numerical values as Number objects. The numbers in typescript are used as both integers as well as floating-point values. The number class acts as wrapper and manipulates the numeric literals as they were objects.

Syntax:

var var_name = new Number(value)

Property:

  • MAX_VALUE: It has the largest possible value 1.7976931348623157E+308 in JavaScript.
  • MIN_VALUE: It has the smallest possible value 5E-324 in JavaScript.
  • NaN: This property has Equal to a value that is not a number.
  • NEGATIVE_INFINITY: This has a value that is less than MIN_VALUE.
  • POSITIVE_INFINITY: This has a value that is greater than MAX_VALUE.
  • prototype: This property is used to assign new properties and methods to the Number object.
  • constructor: This property will return the function that created this object’s instance

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



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

Example of NaN:

filter_none

edit
close

play_arrow

link
brightness_4
code

var day = 0 
if( day<=0 || v >7) { 
   day = Number.NaN 
   console.log("Day is "+ day) 
} else
   console.log("Value Accepted.."
}

chevron_right


Output:

Month is NaN

Methods:

  • toExponential(): This method will return a number to display in exponential notation.
  • toFixed(): This method will stable the number to the right of the decimal with a specific number of digits.
  • toLocaleString(): This method is used to convert the number into a local specific representation of the number.
  • toPrecision(): It will define total digits to the left and right of the decimal to display of a number. It will also show an error when there is negative precision.
  • toString(): Used to return the string representation of the number in the specified base.
  • valueOf(): This method will return number’s primitive value.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The toExponential() 
var num1 = 2525.30 
var val = num1.toExponential(); 
console.log(val)

chevron_right


Output:

2.5253e+3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The toFixed()
var num3 = 237.134 
console.log("num3.toFixed() is "+num3.toFixed()) 
console.log("num3.toFixed(2) is "+num3.toFixed(3)) 
console.log("num3.toFixed(6) is "+num3.toFixed(5))

chevron_right


Output:



num3.toFixed() is 237 
num3.toFixed(2) is 237.134 
num3.toFixed(6) is 237.13400

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The toLocaleString()
var num = new Number( 237.1346); 
console.log( num.toLocaleString());

chevron_right


Output:

237.1346

Example 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The toPrecision()
var num = new Number(5.7645326); 
console.log(num.toPrecision()); 
console.log(num.toPrecision(1)); 
console.log(num.toPrecision(2));

chevron_right


Output:

5.7645326 
5 
5.7

Example 5:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The toString()
var num = new Number(10); 
console.log(num.toString()); 
console.log(num.toString(2)); 
console.log(num.toString(8));

chevron_right


Output:

10 
1010 
12

Example 6:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The valueOf()
var num = new Number(20); 
console.log(num.valueOf());

chevron_right


Output:

20

full-stack-img




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.