JavaScript

JavaScript methods for working with numbers

Another primitive data type that represents positive, negative, float, binary, octal, hexadecimal and exponential values in programming is a Number.

JavaScript provides us with various number methods through which we can manipulate numeric values. In this article, we’ll learn various methods for working with numbers in JavaScript along with examples.

Number Methods

To work with numbers we need specific methods that can help us manipulate and modify their values easily. For this, JavaScript provides us with various methods to work with numeric values.

Following are some JavaScript number methods along with explanations and examples for your better understanding:

Number()

Sometimes a user wants to convert a string into a number. For this purpose, Number() method is available. It’ll return a numeric value that’s converted from the argument passed.

Syntax:

Number(argument);

Example:

// Example 1
let a = '4'
let num = Number(a)
console.log(num)                 // Output: 4
console.log(num * 5)             // Output: 20

// Example 2
let b = '111.11'
let num1 = Number(b)
console.log(num1)                 // Output: 111.11

// Example 3
let c = '44.65'
let num2 = Number(c)
console.log(num2)                 // Output: 44.65

Output:

Furthermore, when boolean values true and false are passed, the program returns 1 and 0 as output.

Example:

console.log(Number(true));          // returns 1
console.log(Number(false));         // returns 0

Output:

toString()

Now, to return a number as a string to String() method is available and It returns any numeric value as a string.

Syntax:

variable.toString();

Example:

// Example 1
let a = 4444
let num = a.toString()
console.log(num)                 // Output: 4444

// Example 2
let b = 44.67
let num1 = b.toString()
console.log(num1)                 // Output: 44.67

// Example 3
let c = 101+99
let num2 = c.toString()
console.log(num2)                 // Output: 200

// Example 4
let d = 11
let num3 = d.toString(2)
console.log(num3)                 // Output: 1011

Output:

Here, in example 4, passing 2 as a parameter will return the binary value of the number.

toLocaleString()

This method converts the number into a string that represents the local language format.

Syntax:

variable.toLocaleString(locales, options)

It takes two parameters, locales which represent the languages in which you need to format your number. Second parameter is optional where you can define some properties.

Example:

let num = 37320932.001;
//US English
console.log(num.toLocaleString('en-US'));    
// Saudi Arabia (Arabic)
console.log(num.toLocaleString('ar-SA'));

Output:

parseInt()

Similar to the Number() method, a string is provided as the argument and as a result it converts it into an integer value.

Syntax:

parseInt(argument);

Example:

// Example 1
let a = '15.3333'
let num1 = parseInt(a)
console.log(num1)                 // Output: 15
// Example 2
let b = '14 hours'
let num2 = parseInt(b)
console.log(num2)                 // Output: 14
// Example 3
let c = 'hour 5'
let num3 = parseInt(c)
console.log(num3)                 // Output: NaN

Output:

Here, in these examples it’s only returning the value before the decimal point. In the second example it returned only 14, but in the third NaN because it’s unable to find the number value in it.

parseFloat()

This method takes in a string and returns the numeric value including the decimal value.

Syntax:

parseFloat(argument);

Example:

// Example 1
let a = '15.3333'
let num1 = parseFloat(a)
console.log(num1)                 // Output: 15.3333
// Example 2
let b = '0.99998'
let num2 = parseFloat(b)
console.log(num2)                 // Output: 0.99998
// Example 3
let c = '4.8 9.0'
let num3 = parseFloat(c)
console.log(num3)                 // Output: 4.8

Output:

Here, in the third example it only returned the first number and not the second. It differs from parseInt() as it also returns the decimal value.

toExponential()

This method is provided with a number as an argument and as an output an exponential value is returned in the form of a string.

Syntax:

variable.toExponential();

Example:

// Example 1
let a = 15.3333
let num1 = a.toExponential();
console.log(num1)                
// Example 2
let b = 0.99998
let num2 = b.toExponential();
console.log(num2)                
// Example 3
let c = 4576.08976
let num3 = c.toExponential(2);
console.log(num3)

Output:

toPrecision()

This method takes in an argument and returns the numeric value with the length given as the argument and If no argument is provided, it simply returns the same value.

Syntax:

variable.toPrecision();

Example:

// Example 1
let a = 15.3333
let num1 = a.toPrecision(4);
console.log(num1)                
// Example 2
let b = 0.99998
let num2 = b.toPrecision(1);
console.log(num2)

Output:

toFixed()

This method rounds up a given number with the specified number of decimals. It takes in an optional parameter that represents the number of digits to be displayed after a decimal point.

Syntax:

variable.toFixed();

Example:

let x = 15.8902;
console.log(x.toFixed());          
console.log(x.toFixed(2));          
console.log(x.toFixed(3));

Output:

Conclusion:

JavaScript comes with various number methods that help us to deal with numeric values.In this article, we learned various methods available in JavaScript for Numbers. These methods help us in changing the values of numbers or return them as a string. There are other methods also available with various functionalities.

Through these methods, users can solve various problems more precisely. Thus, implementation of numerical algorithms becomes an easy task.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.