TypeScript -Number Data Type

Type Number Data Type

TypeScript supports numeric values as Number objects and number object converts numeric literal to an instance of the number class.

The  four types(decimalbinaryoctal and hexadecimal) of number literals are supported by number type.

  

        let decimalNumber: number = 42;
        let binaryNumber: number = 0b101010;
        let octalNumber: number = 0o52;
        let hexadecimalNumber: number = 0x2a;
        
        console.log(decimalNumber);
        console.log(binaryNumber);
        console.log(octalNumber);
        console.log(octalNumber);
    

Decimal numbers

  

        let FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
        let FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
        let FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607

        console.log(FLT_SIGNBIT);
        console.log(FLT_EXPONENT);
        console.log(FLT_MANTISSA);
    
  

        var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
        var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
        var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607

        console.log(FLT_SIGNBIT);
        console.log(FLT_EXPONENT);
        console.log(FLT_MANTISSA);
    

Octal numbers

  

        let n = 0755; // 493
        let m = 0644; // 420
    
  

        0xFFFFFFFFFFFFFFFFF // 295147905179352830000
        0x123456789ABCDEF   // 81985529216486900
        0XA                 // 10
    

Number object

The Number object has properties for numerical constants, such as  MAX_VALUE, MIN_VALUE, NaN  etc.  You cannot change the values of these properties and you use them as follows:

  

        let biggestNum = Number.MAX_VALUE;
        let smallestNum = Number.MIN_VALUE;
        let infiniteNum = Number.POSITIVE_INFINITY;
        let negInfiniteNum = Number.NEGATIVE_INFINITY;
        let notANum = Number.NaN;

    

You always refer to a property of the predefined Number object as above code.

Properties of Number

Property

Description

Number.MAX_VALUE

Return the largest numeric value

Number.MIN_VALUE

Return the smallest number value

Number.NaN

Represents "not a number" value

Number.NEGATIVE_INFINITY

Return value is less than MIN_VALUE, returned on overflow

Number.POSITIVE_INFINITY

 Return value is greater than MAX_VALUE; returned on overflow

Number.EPSILON

Return smallest interval between two representable numbers.

Number.MIN_SAFE_INTEGER

Return Minimum safe integer

Number.MAX_SAFE_INTEGER

Return Maximum safe integer

Methods of Number

Method

Description

Number.parseFloat()

Parses its argument and returns a floating point number Same as the global parseFloat() function.

Number.parseInt()

parses a string and returns a number Same as the global parseInt() function.

Number.isFinite()

Check whether the passed value is a finite number.

Number.isInteger()

Check whether the passed value is an integer.

Number.isNaN()

Check whether the passed value is NaN.

Number.isSafeInteger()

Check whether the provided value is a number that is a safe integer.

Methods of Number.prototype

Method

Description

toExponential()

Returns a string representing the number in exponential notation.

toFixed()

Returns a string representing the number in fixed-point notation.

toPrecision()

Returns a string representing the number to a specified precision in fixed-point notation.