Javascript Number Object

In Javascript number type include floating-point and integer values, its wrapper for primitive numeric values.

A Number object is created using the Number() constructor. In Javascript numbers can be divided into two groups:

Integers - The numbers such as 120, -232, and 5.

floating-point - The fractional numbers such as 4.33, -8.422, and .55

In Javascript primitive number can be declared using the literal notation:

Syntax

<html>
<head>
<title>My first Javascript code</title>
var x = 13.44;     // A number with decimals
var y = 45;       // A number without decimals
</head>
<body>
</body>
</html>

OR, you can use Number global object directly

<html>
<head>
<title>My first Javascript code</title>
var val = new Number(number);
</head>
<body>
</body>
</html>

Number Properties

Number object has following properties that are part of every object.

PropertyDescription
MAX_VALUE Return largest positive representable number in Javascript.
MIN_VALUE The smallest positive representable number in Javascript.
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
NaN Represents a "Not-a-Number" value
POSITIVE_INFINITY Represents positive infinity (returned on overflow)
prototype Allows to add addition of properties and function to all objects
constructor Returns a reference to the function that created the Number object

Number Function

The Number object contains only the default function that are part of every object's definition.

MethodDescription
toExponential() Exponential representation of number.
toFixed() Number of digits to appear after the decimal point.
toLocaleString() Return string with language specific representation of this number.
toPrecision() Defines how many total digits (including digits to the left and right of the decimal) to display of a number.
toString() Returns the string representation of the number's value.
valueOf() Returns the number's value.

Javascript Number Example

<HTML>
<TITLE>Example of Number Object</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
//Example of constructor
var num = new Number( 177.1234 );
document.write("num.constructor() is : " + num.constructor); 
document.write("</br>"); 

//Example of toExponential() function
var num=77.1234;
var val = num.toExponential(); 
document.write("num.toExponential() is : " + val ); 
document.write("</br>"); 
val = num.toExponential(4); 
document.write("num.toExponential(4) is : " + val ); 
document.write("</br>"); 

//Example of toFixed() function
var num=122.211;
document.write("num.toFixed() is : " + num.toFixed()); 
document.write("</br>"); 
document.write("num.toFixed(6) is : " + num.toFixed(6)); 
document.write("</br>"); 

//Example of toLocaleString() function
var num = new Number(177.1234);
document.write("num.toLocaleString() is : " + num.toLocaleString()); 

//Example of toPrecision() function
var num = new Number(7.5678);
document.write("num.toPrecision() is " + num.toPrecision());
document.write("</br>"); 
document.write("num.toPrecision(4) is " + num.toPrecision(4)); 
document.write("</br>"); 

//Example of toString() function
var num = new Number(15);
document.write("num.toString() is " + num.toString());
document.write("</br>"); 
document.write("num.toString(2) is " + num.toString(2)); 
document.write("</br>"); 

//Example of valueOf() function
var num = new Number(15.11234);
document.write("num.valueOf() is " + num.valueOf());
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Above example will produce following result

//outputs of constructor
num.constructor() is : function Number() { [native code] }

//outputs toExponential function
num.toExponential() is : 7.71234e+1
num.toExponential(4) is : 7.7123e+1

//outputs of toFixed function
num.toFixed() is : 177
num.toFixed(6) is : 177.123400

//outputs of toLocaleString function
num.toLocaleString() is : 177.1234

//outputs of toPrecision function
num.toPrecision() is : 7.5678
num.toPrecision(4) is 7.123

//outputs of toString function
num.toString() is 15
num.toString(2) is 1111

//outputs of valueOf function
num.valueOf() is 15.11234