Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Number Methods

JS Number Methods

JavaScript provides the following number methods that can be used on all JavaScript numbers:

  • toString(); Returns a number as a string
  • toExponential(); Returns a number written in exponential notation
  • toFixed(); Returns a number written with a number of decimals
  • toPrecision(); Returns a number written with a specified length
  • ValueOf(); Returns a number as a number
  • Number(); Returns a number converted from its argument
  • parseFloat(); Parses its argument and returns a floating point number
  • parseInt(); Parses its argument and returns a whole number
  • isInteger(); Returns true if the argument is an integer
  • isSafeInteger(); Returns true if the argument is a safe integer.
The 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):

					 
        
          let x = 123;
          x.toString();
          (123).toString();
          (100 + 23).toString();
        
      
Decimal To Other System

By default, JavaScript displays numbers as base 10 decimals. But you can use the toString() method to output numbers from base 2 to base 36. Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.

					 
        
          let myNumber = 32;
          myNumber.toString(32);
          myNumber.toString(16);  // Hexadecimal
          myNumber.toString(12);
          myNumber.toString(10);  // Decimal
          myNumber.toString(8);  // Octaal
          myNumber.toString(2);  // Binary
        
      
The toExponential() Method

toExponential() returns a string, with a number rounded and written using exponential notation. A parameter defines the number of characters behind the decimal point:

					 
        
          let a = 9.656;
          a.toExponential(2);
          a.toExponential(4);
          a.toExponential(6);
        
      
The toFixed() Method

toFixed() returns a string, with the number written with a specified number of decimals. toFixed(2) is perfect for working with money.

					 
        
          let a = 9.656;
          a.toFixed(0);
          a.toFixed(2);
          a.toFixed(4);
          a.toFixed(6);
        
      
The toPrecision() Method

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

					 
        
          let a = 9.656;
          a.toPrecision();
          a.toPrecision(2);
          a.toPrecision(4);
          a.toPrecision(6);
        
      
The valueOf() Method

valueOf() returns a number as a number.

					 
        
          let a = 123;
          a.valueOf();
          (123).valueOf();
          (100 + 23).valueOf();
        
      
The Number() Method

The Number() method can be used to convert JavaScript variables to numbers.

					 
        
          Number(true);
          Number(false);
          Number("10");
          Number("  10");
          Number("10  ");
          Number(" 10  ");
          Number("10.33");
          Number("10,33");
          Number("10 33");
          Number("John");
          Number(new Date("1970-01-01"));
        
      
The parseInt() Method

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

					 
        
          parseInt("-10");
          parseInt("-10.33");
          parseInt("10");
          parseInt("10.33");
          parseInt("10 20 30");
          parseInt("10 years");
          parseInt("years 10");
        
      
The parseFloat() Method

parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.

					 
        
          parseFloat("10");
          parseFloat("10.33");
          parseFloat("10 20 30");
          parseFloat("10 years");
          parseFloat("years 10");
        
      
The Number.isInteger() Method

The Number.isInteger() method returns true if the argument is an integer.

					 
        
          Number.isInteger(10);
          Number.isInteger(10.5);
        
      
The Number.isSafeInteger() Method

A safe integer is an integer that can be exactly represented as a double precision number. The Number.isSafeInteger() method returns true if the argument is a safe integer.

					 
        
          Number.isSafeInteger(10);
          Number.isSafeInteger(12345678901234567890);
        
      
If the number cannot be converted, NaN (Not a Number) is returned.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved