12 Most Common JavaScript Number Methods
There are many JavaScript Number methods that are often used to manipulate number values. These number formats support integers, floating-point numbers, such as decimal, hexadecimal, notations.
Here are a few methods that can make manipulating and modifying numeric values much easier.
JavaScript Number Format
1. Number()
The Number()
method converts a string into a number.
// Example 1
let x = '10'
let num = Number(x)console.log(num) // Output: 10
console.log(num * 9) // Output: 90// Example 2
let x = true
let num = Number(x)console.log(num) // Output: 1
console.log(num + 9) // Output: 10//Example 3
let x = false
let num = Number(x)console.log(num) // Output: 0
console.log(num + 9) // Output: 9
In the example above, Number(value)
method is used to convert string x
to an integer and perform operations on the number value. Setting x
totrue
returns 1 and false
returns 0.
2. parseInt()
.parseInt()
very similar to the number()
method, parseInt()
formats a string into an integer.
// Example 1
let x = '10.99'
let num = parseInt(x)console.log(num) // Output: 10// Example 2
let x = '7 days'
let num = parseInt(x)console.log(num) // Output: 7// Example 3
let x = 'day 7'
let num = parseInt(x)console.log(num) // Output: NaN
The parseInt(value)
method takes a string x
, a decimal number, and returns an integer. It returns the first integer 7, of the value ‘7 days’. If we change the value of x
to ‘day 7’, it will return NaN
because the method cannot find the number value.
3. parseFloat()
The .parseFloat()
method parses a string value and returns the number with its decimal value.
// Example 1
let x = '10.99'
let num = parseFloat(x)console.log(num) // Output: 10.99// Example 2
let x = '2.49 3.99'
let num = parseFloat(x)console.log(num) // Output: 2// Example 3
let x = 'day 7'
let num = parseFloat(x)console.log(num) // Output: NaN
Similar to parseInt()
, parseFloat()
checks to find the first character in the string is a number, if yes, it parses the string and returns it as a number with the decimal value. The main reason that sets parseInt()
and parseFloat()
methods apart is that parseFloat()
after parsing the string, returns the number with its decimal values, whereas parseInt()
only returns the integer.
4. toString()
The .toString()
method converts a numeric value into a string.
// Example 1
let x = 10
let num = x.toString()console.log(num) // Output: '10'// Example 2
let x = 10
let num = x.toString(2)console.log(num) // Output: 1010
Adding the number 2
as a parameter to .toString()
method, returns the binary value of the number.
5. toExponential()
As the name suggests, .toExponential()
converts a number into a string and returns it in an exponential format.
// Example 1
let x = 456.789
let num = x.toExponential()console.log(num) // Output: 4.56789e+2// Example 2
let x = 456.789
let num = x.toExponential(2)console.log(num) // Output: 4.57e+2
The parameter with 2 digits, returns the value with two decimal digits.
6. toFixed()
method rounds up a number to the nearest highest or lowest fixed-point notation. It takes in a parameter which signifies the number of digits should be displayed after the decimal point.
// Example 1
var num = 4.56789;console.log(num.toFixed()) // Output : 5// Example 2
var num = 4.56789;
console.log(num.toFixed(2)) // Output : 4.57
In the example above, the .toFixed(digits)
method formats the number with 2 number of digits after the decimal point. If you call the .toFixed()
method without a parameter, it returns a rounded-up integer.
7. toPrecision()
.toPrecision()
returns the numeric value with a specific length. It takes an argument that signifies the length. If given without a specific length, the method returns the number as it is.
// Example 1
var num = 456.789;
console.log(num.toPrecision()) // Output : 456.789// Example 2
var num = 456.789;
console.log(num.toPrecision(2)) // Output : 4.6
8. valueOf()
The valueOf()
method is used to return the primitive value of the Number
object you’re calling it on. Primitive types in JavaScript are number, string, bigint, symbol, undefined, null, and boolean.
let x = 45
let num = x.valueOf()console.log(num) // Output: 45console.log(typeof num); // Output: Number
9. toLocaleString()
The toLocaleString()
method uses a local language format to convert a number and returns it as a string. It takes two arguments, locales
and options
, which defines the language of which conversion you want to use, and the behavior of the function.
obj.toLocaleString([locales[, options]])
let num = 226537.883;//US English
console.log(num.toLocaleString('en-US')); //Output: 226,537.883// Romanian (Romania)
console.log(num.toLocaleString('ro-RO')); //Output: 226.537,883// Standard French (especially in France)
console.log(num.toLocaleString('fr-FR')); //Output: 226 537,883
10. isInteger()
.isInteger()
checks whether the given value is an integer and returns a boolean value.
//Example 1
let x = 10
let num = Number.isInteger(x)console.log(num) // Output: true//Example 2
let x = 10.99
let num = Number.isInteger(x)console.log(num) // Output: false//Example 3
let x = "10"
let num = Number.isInteger(x)console.log(num) // Output: false
In the examples above, setting x
to 10
returns a boolean value true
; when we change the value to 10.99
it returns false
because the value has decimal digits, which means it’s not an integer. Similarly, when we wrap the value with quotations, the method returns false
because the value is a string.
11. isFinite()
.isFinite()
checks whether the given value is finite and returns a boolean value.
//Example 1
let x = 10
let num = Number.isFinite(x)console.log(num) // Output: true//Example 2
let x = -10.99
let num = Number.isFinite(x)console.log(num) // Output: true//Example 3
let x = "10"
let num = Number.isFinite(x)console.log(num) // Output: false
The inFinite()
method returns true
when set x
to 10
, -10.99
because these are finite numbers. Returns false
when we change the value to a string.
12. isSafeInteger()
.isSafeInteger()
checks whether the given value is a safe integer and returns a boolean value. An integer is considered as safe integer when all integers are exactly between (2^53–1) to -(2^53–1))
Number.isSafeInteger(220) //Output: true
Number.isSafeInteger(-220) //Output: true
Number.isSafeInteger(2.2) //Output: false
Number.isSafeInteger(978678367894123469410320213) //Output: false
These are the 12 JavaScript number formats that can help you manipulate the number values and make your code more efficient.
Previous posts:
12 Must KnowArray Methods for the Next Interview — JavaScript
Arrays: Left Rotation (JavaScript)
How to deploy Rails App on Heroku?
How to Make Your LinkedIn Profile Stand Out & Get Better Opportunities in 2020?
How to Create Simple Forms in React?
How To Solve Loop Detection Interview Question? | Crack The Coding Interview (6th edition)
Markdown — A Easier & Fast to Learn Markup Language
Data Structure — Linked List| SINGLY LINKED LIST PRACTICE(LeetCode)
Debug JS Code with DevTool Network Tab