Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Number Prototype Property

  • Last Updated : 14 Sep, 2020
Geek Week

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.

  1. 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:




    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"
    
  2. Number.prototype.valueOf(): It returns the value of the object specified. This method is called by JavaScript internally.

    Example:




    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"
    
  3. 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:




    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"
    
  4. 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:




    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;
    }
    //Display octal of 233
    console.log(octa_test(233));
     //Display octal of 65 
    console.log(octa_test('65')); 

    Output:

    "351"
    "101"
    
  5. 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:




    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"
    
  6. 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:




    var number = 10000;
    console.log(number.toLocaleString());
      
    // German uses comma as decimal separator
    // and period for thousands
    console.log(number.toLocaleString('de-DE'
      { style: 'currency', currency: 'EUR' }));
      
    // India uses thousands/lakh/crore separators
    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'); //German
    }
    function test2(x){
      return x.toLocaleString('ar-EG');  //Arabic
    }
    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"
    "??? ???"
    

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :