How to use Number Methods in JavaScript?

Number Methods

  • toString ( )
  • toExponential ( )
  • toFixed ( )
  • toPrecision ( )
  • valueOf( )
  • isFinite( )
  • isInteger( )
  • isNan( )
  • isSafeInteger( )

toString( )

toString ( ) method returns a number as a string in other words it converts a number into a string. We can use this method to output numbers as hexadecimal (16), octal(8), binary(2).

Syntax: –
Variable_name.toString( );

toExponential ( )

The toExponential() method converts a number into an exponential notation.

Syntax:-
Variable_name.toExponential(y)
Where y is an integer between 0 and 20 representing the number of digits in the
notation after the decimal point. If omitted, it is set to as many digits as necessary to
represent the value.

Ex: –

toFixed ( )

The toFixed() method converts a number into a string, keeping a specified number of decimals also rounds the decimal. If the desired number of decimals is higher than the actual number, nulls are added to create the desired decimal length.

Syntax: –
a.toFixed(y)
Where y is the number of digits after the decimal point. Default is 0 (no digits after the decimal point)

Syntax: –
a.toFixed(y)
Where y is the number of digits after the decimal point. Default is 0 (no digits after the decimal point)

Ex:-

toPrecision ( )

The toPrecision() method formats a number to a specified length.
A decimal point and nulls are added (if needed), to create the specified length.

Syntax:-
Variable_name.toPrecision(y)
Where y is the number of digits. If omitted, it returns the entire number (without any
formatting)

Syntax:-
Variable_name.toPrecision(y)
Where y is the number of digits. If omitted, it returns the entire number (without any
formatting)

Ex:-

Number.isNaN()

The Number.isNaN() method determines whether a value is NaN (Not-A-Number).
This method returns true if the value is of the type Number, and equates to NaN. Otherwise, it returns false.
Number.isNaN() is different from the global isNaN() function. The global isNaN() function
converts the tested value to a Number, then tests it.
Number.isNaN() does not convert the values to a Number, and will not return true for any value
that is not of the type Number.

Number.isInteger( )

The Number.isInteger() method determines whether a value is an integer.
This method returns true if the value is of the type Number, and an integer, otherwise it
returns false.

Ex: –

Number.isSafeInteger()

The Number.isSafeInteger() method determines whether a value is a safe integer.
A safe integer is an integer that can be exactly all integers from (2^53 – 1) to -(2^53 – 1)
This method returns true if the value is of the type Number and a safe integer.
Otherwise, it returns false.

Tagged : / / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x