JavaScript Number Prototype Property
Last Updated :
10 Feb, 2023
Number object in JavaScript is used to manipulate numbers whether it is negative or positive. The JavaScript Number type represents the double (fractional values) type numbers. A number literal like 98 is not an integer value but a floating-point value. It maintains the precision of 17 decimal places. 1.8 * 10^308 is the largest value it can hold, any numbers beyond this value is replaced with a constant known as Infinity.
Number.prototype refers to the Number() object and not to a single number object. The prototype property adds the following instance methods to number object.
Number.prototype.toPrecision(precision_value): It returns a string that represents a number to stated precision in exponential notation or fixed-point notation. The precision_value parameter specifies the number of digits in which the number need to be represented. The value of the parameter ranges from 0 to 20, and if it is not in the given range, it produces an exception known as RangeError.
Example:
javascript
let num1 = 64.67890
let num2 = 0.000123
console.log(num1.toPrecision())
console.log(num1.toPrecision(3))
console.log(num1.toPrecision(1))
console.log(num2.toPrecision())
console.log(num2.toPrecision(3))
console.log(num2.toPrecision(1))
console.log((100000.02).toPrecision(4))
|
Output:
"64.6789"
"64.7"
"6e+1"
"0.000123"
"0.000123"
"0.0001"
"1.000e+5"
Javascript
function test(x) {
return Number.parseFloat(x).toPrecision(5);
}
console.log(test(4532.567));
console.log(test(0.00065));
console.log(test( '4.44e+5' ));
console.log(test( 'geeksforgeeks' ));
|
Output:
"4532.6"
"0.00065000"
"4.4400e+5"
"NaN"
Number.prototype.valueOf(): It returns the value of the object specified. This method is called by JavaScript internally.
Example:
javascript
let num1 = new Number(105)
console.log(num1)
console.log( typeof num1)
let num2 = num1.valueOf()
console.log(num2)
console.log( typeof num2)
|
Output:
105
"object"
105
"number"
Example:
Javascript
let num1 = new Number(100)
console.log(num1)
console.log( typeof num1)
let num2 = num1.valueOf()
console.log(num2)
console.log( typeof num2)
|
Output:
100
"object"
100
"number"
Number.prototype.toFixed(num): It represents the number in fixed-point notation. The num parameter specifies the number of digits after the decimal point. It may support a substantial range of values. The value of the parameter ranges from 0 to 100, and if it is not in the given range, it produces an exception known as RangeError. If this method is invoked on an object other than Number, it produces an exception known as TypeError. The default value of the parameter is zero, and the number object may get rounded.
Example:
javascript
let num = 134567.8845
console.log(num.toFixed())
console.log(num.toFixed(1))
console.log(num.toFixed(7))
console.log((2.56e+10).toFixed(3))
console.log(9.88.toFixed(1))
|
Output:
"134568"
"134567.9"
"134567.8845000"
"2560000000.000"
"9.9"
Example:
Javascript
function test(x) {
return Number.parseFloat(x).toFixed(4);
}
console.log(test(56.7645));
console.log(test(0.56400));
console.log(test( 'geeksforgeeks' ));
|
Output:
"56.7645"
"0.5640"
"NaN"
Number.prototype.toString([base]): It returns a string that represents the number in the specified base value. The parameter base specifies the digit ranging between 2 to 36 to represent the number in that particular base. If the base values are not in the specified range, it generates RangeError exception. If the base is not specified, its default value will be 10. If the number object is a negative value, then it does not represents its 2’s complement rather it preserves the sign and puts it after representing the number in the specified base.
Example:
javascript
let count = 50
console.log(count.toString(8))
console.log((12.2).toString(2))
console.log((12.2).toString())
console.log((343).toString(16))
console.log((-5).toString(2))
console.log((-0xff).toString(2))
|
Output:
"62"
"1100.001100110011001100110011001100110011001100110011"
"12.2"
"157"
"-101"
"-11111111"
Example:
Javascript
function octa_test(c) {
if (c < 256) {
return Math.abs(c).toString(8);
}
return 0;
}
console.log(octa_test(233));
console.log(octa_test( '65' ));
|
Output:
"351"
"101"
Number.prototype.toExponential(fraction_num): It represents the number in exponential notation. The frac_num parameter species the number of digits after the decimal point. The value of the parameter ranges from 0 to 20, and if it is not in the given range, it produces an exception known as RangeError. If this method is invoked on an object other than Number, it produces an exception known as TypeError. If the frac_num parameter is not specified, the number of digits following the decimal point is selected so that number is represented uniquely.
Example:
javascript
var num1 = 123.4567;
var num2 = 10000;
console.log(num1.toExponential());
console.log(num2.toExponential());
console.log(num1.toExponential(4));
console.log(num2.toExponential(4));
console.log(100 .toExponential());
|
Output:
"1.234567e+2"
"1e+4"
"1.2346e+2"
"1.0000e+4"
"1e+2"
Example:
Javascript
function test(x, f) {
return Number.parseFloat(x).toExponential(f);
}
console.log(test(100000, 3));
console.log(test( '100000' ));
console.log(test( 'geeksforgeeks' ));
|
Output:
"1.000e+5"
"1e+5"
"NaN"
Number.prototype.toLocaleString([locales [, options]]): It represents the number in the language specified. The parameter locales and options specify the languages whose formatting convention is to be used, both the parameters decide the function’s behavior.
Example:
javascript
var number = 10000;
console.log(number.toLocaleString());
console.log(number.toLocaleString( 'de-DE' ,
{ style: 'currency' , currency: 'EUR' }));
console.log(number.toLocaleString( 'en-IN' ,
{ maximumSignificantDigits: 3 }));
|
Output:
"10, 000"
"10.000, 00 €"
"10, 000"
Example:
Javascript
function test(x){
return x.toLocaleString( 'de-DE' );
}
function test2(x){
return x.toLocaleString( 'ar-EG' );
}
console.log(test(1765890.654));
console.log(test( '1765890.654' ));
console.log(test(NaN));
console.log(test2(1765890.654));
console.log(test2( '1765890.654' ));
console.log(test2(NaN));
|
Output:
"1.765.890, 654"
"1765890.654"
"NaN"
"?????????????"
"1765890.654"
"??? ???"
We have a Cheat Sheet on Javascript Numbers where we covered all the important topics of Javascript to check those please go through JavaScript Number Complete Reference.
Similar Reads
JavaScript Error.prototype.lineNumber Property
In JavaScript, the Error.prototype.lineNumber property helps us to determine which line in our code corresponds to an error. One important thing to note is that this property is not used extensively as it is not a standard feature. Syntax:errorVariable.lineNumber Example 1: C/C++ Code var ex_variabl
1 min read
JavaScript String prototype Property
The prototype property allows to add new properties and methods to the existing JavaScript object types. There are two examples to describe the JavaScript String prototype property. Syntax: object.prototype.name = valueReturn Value: It returns a reference to the String.prototype object. Example 1: T
2 min read
JavaScript Number Static Properties
The JavaScript Number is an object that can represent numeric values like integer, float, hexadecimal, decimal, etc. It acts as a wrapper object for primitive numeric values. It has various methods and properties that are used to perform different tasks on numbers. The following table shows some sta
2 min read
JavaScript Number NaN Property
NaN, which stands for "Not a Number," is a special value in JavaScript that shows up when a mathematical operation can't return a valid number. This can happen if you try something like dividing zero by zero or converting a string that doesn't contain numbers. NaN helps you spot when something went
2 min read
JavaScript Number.MIN_VALUE Property
Number.MIN_VALUE is the smallest possible positive number representable in JavaScript within float precision. It does not represent the most negative number, instead of that, it possesses the value of the smallest positive number which is closest to 0. Syntax: Number.MIN_VALUEReturn Value: It has ap
2 min read
JavaScript Number.MAX_VALUE Property
Number.MAX_VALUE represents the biggest possible numeric value of a positive number that can be represented in JavaScript. It is the maximum possible positive number representable in JavaScript within float precision. The values greater than Max_VALUE are represented as infinity. Syntax: Number.MAX_
2 min read
JavaScript Object.prototype.constructor Property
A constructor in JavaScript is a special function used to create and initialize objects. It sets up object properties and is typically invoked using the new keyword. Constructors allow for the creation of multiple instances with similar properties and methods. In JavaScript, constructors can be defi
8 min read
JavaScript Object Prototypes
JavaScript prototypes are used to access the properties and methods of objects. Inherited properties are originally defined in the prototype or parent object. The Date object is inherited from Date.prototype, Array object inherits from Array.prototype, etc. The prototypes may be used to add new prop
1 min read
JavaScript Object setPrototypeOf() Method
The Object.setPrototypeOf() method in JavaScript is a standard built-in object thatthat will sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null. Syntax:Object.setPrototypeOf(obj, prototype)Parameters:This method accepts two parameters as me
3 min read
JavaScript Number.MIN_SAFE_INTEGER Property
The JavaScript Number.MIN_SAFE_INTEGER is a constant number that represents the minimum safe integer. This constant has a value of (-(253 - 1)). Use Number.MIN_SAFE_INTEGER, as a property of a number object because it is a static property of a number. Syntax: Number.MIN_SAFE_INTEGER Return Value: Co
1 min read