WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Number Methods


Number strategies help you to work with numbers.


Global Methods

JavaScript worldwide capacities can be utilized on all JavaScript information types.

These are the most significant techniques, when working with numbers:

Method Description
Number() Returns a number, changed over from its argument.
parseFloat() Parses its contention and returns a gliding point number
parseInt() Parses its contention and returns an integer

Number Methods

JavaScript number strategies are techniques that can be utilized on numbers:

Method Description
toString() Returns a number as a string
toExponential() Returns a string, with a number adjusted and composed utilizing exponential notation.
toFixed() Returns a string, with a number adjusted and composed with a predefined number of decimals.
toPrecision() Returns a string, with a number composed with a predefined length
valueOf() Returns a number as a number

The toString() Method

toString() restores a number as a string.

All number strategies can be utilized on a numbers (literals, factors, or expressions):

Example

var x = 123;
x.toString();           /returns 123 from variable x
(123).toString();       /returns 123 from exacting 123
(100 + 23).toString();  /returns 123 from articulation 100 + 23
Try it Yourself »

The toExponential() Method

toExponential() restores a string, with a number adjusted and composed utilizing exponential notation.

A parameter characterizes the quantity of characters behind the decimal point:

Example

var x = 9.656;
x.toExponential(2);    /returns 9.66e+0
x.toExponential(4);    /returns 9.6560e+0
x.toExponential(6);    /returns 9.656000e+0
Try it yourself »

The parameter is discretionary. In the event that you don't indicate it, JavaScript won't round the number.


The toFixed() Method

toFixed() restores a string, with the number composed with a predefined number of decimals:

Example

var x = 9.656;
x.toFixed(0);          /returns 10
x.toFixed(2);          /returns 9.66
x.toFixed(4);          /returns 9.6560
x.toFixed(6);          /returns 9.656000
Try it yourself »

The toPrecision() Method

toPrecision() restores a string, with a number composed with a determined length:

Example

var x = 9.656;
x.toPrecision();        //returns 9.656
x.toPrecision(2);       //returns 9.7
x.toPrecision(4);       //returns 9.656
x.toPrecision(6);       //returns 9.65600
Try it Yourself »

Converting Variables to Numbers

There are 3 JavaScript capacities that can be utilized to change over factors to numbers:

  • The Number() method
  • The parseInt() method
  • The parseFloat() method

These techniques are not number strategies, however global JavaScript methods.


The Number() Method

Number(), can be utilized to change over JavaScript factors to numbers:

Example

x = true;
Number(x);        //returns 1
x = false;     
Number(x);        //returns 0
x = new Date();
Number(x);        //returns 1404568027739
x = "10"
Number(x);        //returns 10
x = "10 20"
Number(x);        //returns NaN
Try it Yourself »

The parseInt() Method

parseInt() parses a string and returns an entire number. Spaces are permitted. Just the primary number is returned:

Example

parseInt("10");        /returns 10
parseInt("10.33");     /returns 10
parseInt("10 20 30");  /returns 10
parseInt("10 years");  /returns 10
parseInt("years 10");  /returns NaN 
Try it yourself »

If the number can't be changed over, NaN (Not a Number) is returned.


The parseFloat() Method

parseFloat() parses a string and returns a number. Spaces are permitted. Just the primary number is returned:

Example

parseFloat("10");       /returns 10
parseFloat("10.33");     //returns 10.33
parseFloat("10 20 30");  //returns 10
parseFloat("10 years");  //returns 10
parseFloat("years 10");  //returns NaN
Try it yourself »

If the number can't be changed over, NaN (Not a Number) is returned.


The valueOf() Method

valueOf() restores a number as a number.

Example

var x = 123;
x.valueOf();           /returns 123 from variable x
(123).valueOf();       /returns 123 from strict 123
(100 + 23).valueOf();  /returns 123 from articulation 100 + 23
Try it Yourself »

In JavaScript, a number can be a crude esteem (typeof = number) or an object (typeof = object).

The valueOf() strategy is utilized inside in JavaScript to change over Number items to crude values.

There is no motivation to utilize it in your code.




Welookups is optimized for learning.Copy right 2018 - 2019 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.

welookups.com