0% found this document useful (0 votes)
10 views8 pages

Number Methods

The document provides an overview of number methods in JavaScript, explaining how primitive values can utilize methods and properties as objects. It details various methods such as toString(), valueOf(), toExponential(), toFixed(), and toPrecision(), along with global methods like Number(), parseInt(), and parseFloat() for converting values to numbers. Additionally, it outlines number properties like MAX_VALUE, MIN_VALUE, and NaN, emphasizing that these properties cannot be used on variables directly.

Uploaded by

Chandra Malli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Number Methods

The document provides an overview of number methods in JavaScript, explaining how primitive values can utilize methods and properties as objects. It details various methods such as toString(), valueOf(), toExponential(), toFixed(), and toPrecision(), along with global methods like Number(), parseInt(), and parseFloat() for converting values to numbers. Additionally, it outlines number properties like MAX_VALUE, MIN_VALUE, and NaN, emphasizing that these properties cannot be used on variables directly.

Uploaded by

Chandra Malli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Number Methods

Number methods help you work with numbers.

Number Methods and Properties


Primitive values (like 2.781 or 2020), cannot have properties and methods (because
they are not objects).
But with JavaScript, methods and properties are also available to primitive values,
because JavaScript treats primitive values as objects when executing methods and
properties.

toString() Method
The toString() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or
expressions):
Example
<script>
let x = 101;
let st = [Link]();
[Link]( st +"<br>"); //returns "101"
[Link]( (45.88).toString() +"<br>"); //returns
"45.88"
[Link]( (39+60).toString() +"<br>"); //returns
"99"
</script>

valueOf() Method
valueOf() returns a number as a number.

Example
<script>
var n = 111;
[Link]( [Link]() ); // returns 111
[Link]( ("111").valueOf() ); // returns 111
[Link]( (111 + 111).valueOf() ); // returns 222
</script>

In JavaScript, a number can be a primitive value (typeof = number) or an object


(typeof = object).

The valueOf() method is used internally in JavaScript to convert Number objects to


primitive values.

There is no reason to use it in your code.

All JavaScript data types have a valueOf() and a toString() method.

toExponential() Method
toExponential() returns a string, with a number rounded and written using
exponential notation.
A parameter defines the number of digits behind the decimal point:
Example
<script>
var n = 1000000000;
[Link]( [Link]() +"<br>"); //returns 1e+7
(def is 0)
[Link]( [Link](2) +"<br>"); //returns
1.00e+7
[Link]( [Link](5) +"<br>"); //returns
1.00000e+7
</script>

The parameter is optional. If you don't specify it, JavaScript will not round the int.

toFixed() Method
toFixed() returns a string, with the number written with a specified number of
decimals:
Example
<script>
let n = 10000.7829;
[Link]( [Link](0) +"<br>" ); // returns 5 (by default
is 0)
[Link]( [Link](1) +"<br>"); // returns 4.8
[Link]( [Link](5) +"<br>"); // returns 4.78290
[Link]( new Number([Link](1)).toExponential(1) +"<br>");
[Link]( (10/4).toFixed(2) );
</script>

toPrecision() Method
toPrecision() returns a string, with a number written with a specified length:

Example
<script>
let n = 10000.7829; //width of value is 9digit
[Link]( [Link](5) +"<br>"); // returns 10001
[Link]( [Link](7) +"<br>"); // returns 10000.78
[Link]( [Link](10) +"<br>"); // returns
10000.78290
</script>

Converting Variables to Numbers


There are 3 JavaScript methods that can be used to convert variables to numbers:
 The Number() method
 The parseInt() method
 The parseFloat() method
These methods are not number methods, but global JavaScript methods.

Global Methods
JavaScript global methods can be used on all JavaScript data types.
These are the most relevant methods, when working with numbers:
Method Description

Number() Returns a number, converted from its argument.

parseFloat() Parses its argument and returns a floating point number

parseInt() Parses its argument and returns an integer

Number() Method
Number() can be used to convert different types of values to number format:

Example
<script>
var a=Number(true);
[Link]( typeof a +"<br>"); //number
[Link]( a +"<br>"); //returns 1
[Link]( Number(false) +"<br>"); //returns 0
[Link]( Number(null) +"<br>"); //returns 0
[Link]( Number("30") +"<br>"); //returns 30
[Link]( Number(" 30 ") +"<br>"); //returns 30
[Link]( Number("30.33") +"<br>"); //returns 30.33
[Link]( Number("30,33") +"<br>"); //returns NaN
[Link]( Number("30 33") +"<br>"); //returns NaN
[Link]( Number("Siva") +"<br>"); //returns NaN
</script>
Note: If the number cannot be converted, NaN (Not a Number) is returned.

Number() Method Used on Dates


Number() can also convert a date to a number:

Example
<script>
let d=new Date();
[Link]( Number(d) ); // returns value in misec
</script>

Note: The Number() method above returns the number of milliseconds since
1-Jan-1970 [Link].0

parseInt() Method
parseInt() parses a string and returns a whole number. Spaces are allowed. Only
the first number is returned:

Example
<script>
let a=parseInt("10");
[Link]( a +"<br>"); // returns 10
[Link]( typeof a +"<br>"); // returns 10
[Link]( parseInt("10.33") +"<br>"); // returns 10
[Link]( parseInt("30 33") +"<br>"); // returns 30
[Link]( parseInt("10 apples") +"<br>"); // returns 10
[Link]( parseInt("mangos 50") +"<br>"); // returns NaN
[Link]( parseInt("siva") +"<br>"); // returns NaN
[Link]( parseInt(true) +"<br>"); // returns NaN
</script>

Note: If the number cannot be converted, NaN (Not a Number) is returned.

parseFloat() Method
parseFloat() parses a string and returns a number. Spaces are allowed. Only the
first number is returned:
Example
<script>
let a=parseFloat("10");
[Link]( a +"<br>"); // returns 10
[Link]( typeof a +"<br>"); // returns 10
[Link]( parseFloat("10.33") +"<br>"); // returns 10.33
[Link]( parseFloat("30 33") +"<br>"); // returns 30
[Link]( parseFloat("10 apples") +"<br>"); // returns 10
[Link]( parseFloat("mangos 50") +"<br>"); // returns NaN
[Link]( parseFloat("siva") +"<br>"); // returns NaN
[Link]( parseFloat(true) +"<br>"); // returns NaN
</script>

Note: If the number cannot be converted, NaN (Not a Number) is returned.

Number Properties
Property Description

MAX_VALUE Returns the largest number possible in JavaScript

MIN_VALUE Returns the smallest number possible in JavaScript

POSITIVE_INFINITY Represents infinity (returned on overflow)

NEGATIVE_INFINITY Represents negative infinity (returned on overflow)

NaN Represents a "Not-a-Number" value

MAX_VALUE returns the largest possible number.

Ex var n = Number.MAX_VALUE;

MIN_VALUE returns the lowest possible number.


Ex var n = Number.MIN_VALUE;

POSITIVE_INFINITY is returned on overflow

Ex var n = Number.POSITIVE_INFINITY;

Ex var n = 1 / 0;

NEGATIVE_INFINITY is returned on overflow:

Ex var x = Number.NEGATIVE_INFINITY;

Ex var x = -1 / 0;

NaN is a JavaScript reserved word indicating that a number is not a legal number.

Ex var n = [Link];
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):

Ex var n = 100 / "a"; // NaN

Example
<script>
let a = Number.MAX_VALUE;
[Link]( a +"<br>");
[Link]( Number.MIN_VALUE + "<br>");

[Link]( Number.POSITIVE_INFINITY +"<br>");


let b = 20 / 0;
[Link]( b +"<br>");

[Link]( Number.NEGATIVE_INFINITY +"<br>");


let c = -10 / 0;
[Link]( c +"<br>");

[Link]( [Link] +"<br>");


let d = Number("ram");
[Link]( d +"<br>");
</script>

Number Properties Cannot be Used on Variables


Number properties belongs to the JavaScript's number object wrapper
called Number.

These properties can only be accessed as Number.MAX_VALUE.

Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value,


will return undefined:

Ex var n = 6;
var m = n.MAX_VALUE; // m becomes undefined

You might also like