JavaScript Number Methods

Introduction

The JavaScript number methods help you to work with numbers. JavaScript can work with various numeric values like integer, float, hexadecimal, decimal, exponential and octal values.

We will explain Javascript number methods in simple maner with examples.

JavaScript Number Methods
Photo by Atypeek Dgn on Pexels.com

The toString() method.

This method converts the number into string or it returns a number as a string.

//Example

var x = 123;
// x is number

console.log(typeof(x));

//output: "number"

var y = x.toString()
// convert to string

console.log(typeof(y));

//output: "string"

//

The toExponential() Method

Exponential notation is a method of writing numbers. 

Exponential numbers take the form Xn, where X is multiplied by itself n times.

A simple example is 8 = 23 = 2×2×2.

In JavaScript toExponential() method returns a string with rounded number writen in exponnential form.

//Example

var x = 323.34;
var y = x.toExponential();
console.log(y);

//output : 3.2334e+2

//

The toFixed() Method

The toFixed() method converts a given number into a string. It rounds to a specified number of decimals. Additional zeros are added to create the desired decimal length if the number of decimals are higher then the actual number.

//Example 

var x = 323.3433;
var y = x.toFixed(2);
console.log(y);

//same as
//x.toFixed(0);           // returns 4
//x.toFixed(2);           // returns 3.66

//output: 323.34

//Example: if the given length in parameters is larger
var x = 3.656;
console.log(x.toFixed(4));
// output: 3.6560

console.log(x.toFixed(6));
//output:  3.656000

//

The toPrecision() Method

The toPrecision() method format a number to a specified length with the given number of significant digits. It rounds the result where necessary. This method returns value as a string.

Here is simple example

//example

var x = 7.938;
x.toPrecision();        // output 7.938
x.toPrecision(2);       // output 7.9
x.toPrecision(4);       // output  7.938
x.toPrecision(6);       // output 7.93800

//

The valueOf() Method

The valueOf() method returns the primitive value of a String object.

This method is called internally by JavaScript , and not explicitly in code. All JavaScript data types have a valueOf() and a toString() method.

Read more here

//example

var x = 444;
x.valueOf();            // output 444 from variable x

//

How to convert String variables to Numeric in JavaScript?

There are 3 JavaScript methods that can be used to convert string variables to numbers.
Number() method, parseInt() method and parseFloat() method

JavaScript global methods 

The methods mentioned above are JavaScript global methods. this includes:

  • Number() Method
  • parseInt() method
  • parseFloat() method

Number() Method

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

consider the following example

// Example

Number(true);
// output: 1

Number(false);
// output: 0

Number("20");
// output: 20

Number(" 20  ");
// output: 20

Number("20.43");
// output: 20.43

Number("20,33"); 
// output:  NaN (Not a Number)

Number("20 33");
// output: NaN

Number("John Doe");
// output: NaN


//

The Number() method is also used to convert Dates to numbers. It returns the number of milliseconds since 1-1-1970.

// Example

let dateToNumber  = Number(new Date("2021-01-01"));
console.log(dateToNumber);
// output: 1609459200000


//

parseInt() method

parseInt() parses a string and returns a whole number. Spaces are allowed to use as parameters but it returns only the first number.

This method returns NaN ( not a number ) if the given string is impossible to convert to a number.

// Example

parseInt("20");
// output: 20

parseInt("20.73");
//output: 20

parseInt("20 70");
// output: 20

parseInt("20 Days");
// output: 20

parseInt("Date 20");
// output: NaN 


//

parseFloat() Method

The parseFloat() Method in JavaScript parses a string and returns a floating-point number. It also returns NaN if the given string is impossible to convert to a string.

// Example

parseFloat("20");        
// output: 20

parseFloat("20.71");     
// output: 20.71

parseFloat("20 10");  
// output: 20

parseFloat("20 Days");  
// output: 20

parseFloat("Number 20");  
// output: NaN

//

Conclusion on JavaScript Number Methods

The above topic covers all the fundamental topics about JS methods and function about Numbers

Numeric dates are represented by the Number object, which can be either integers or floating-point numbers.

The Number object in JavaScript is a wrapper for primitive numeric values.

There is only one kind of number data type in JavaScript, and it does not differentiate between integers and floating-point values.

Download “JavaScript Number Methods” as a PDF

All JavaScript number Methods in one video

References

https://www.w3schools.com/js/js_number_methods.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

https://www.javatpoint.com/javascript-number

https://www.tutorialspoint.com/javascript/javascript_number_object.htm

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *