Skip to main content

JavaScript Number Methods

By SamK
0
0 recommends
Category(s)
Topic(s)

In this tutorial, you'll learn about various string methods in JavaScript.

The toString() Method

The toString() method converts a number into its string representation.

You can use this method on numbers, whether they are literals, variables, or expressions.

let number = 456;
console.log(number.toString()); // 456
console.log((789).toString()); // 789
console.log((200 + 56).toString()); // 256

The toExponential() Method

The toExponential() method returns a string representing a number in exponential notation. You can specify the number of decimal places.

let number = 7.3245;
console.log(number.toExponential()); // 7.3245e+0
console.log(number.toExponential(2)); // 7.32e+0
console.log(number.toExponential(4)); // 7.3245e+0

If no parameter is provided, the full number is returned without rounding.

The toFixed() Method

The toFixed() method formats a number with a fixed number of decimal places.

let num = 5.6789;
console.log(num.toFixed(0)); // 6
console.log(num.toFixed(2)); // 5.68
console.log(num.toFixed(4)); // 5.6789
console.log(num.toFixed(6)); // 5.678900

Using toFixed(2) is useful for displaying currency values with two decimal places.

The toPrecision() Method

The toPrecision() method formats a number to a specified length, including digits before and after the decimal.

let value = 9.876;
console.log(value.toPrecision()); // 9.876
console.log(value.toPrecision(2)); // 9.9
console.log(value.toPrecision(4)); // 9.876
console.log(value.toPrecision(6)); // 9.87600

The valueOf() Method

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

let num = 321;
console.log(num.valueOf()); // 321
console.log((123).valueOf()); // 123
console.log((150 + 50).valueOf()); // 200

The Number() Function

The Number() function converts values into numbers.

console.log(Number(true)); // 1
console.log(Number(false)); // 0
console.log(Number("20")); // 20
console.log(Number("  30")); // 30
console.log(Number("10.55")); // 10.55
console.log(Number("10,55")); // NaN
console.log(Number("Ten")); // NaN

The Number() Method with Dates

The Number() function can also be used to convert dates into numeric timestamps.

let date = new Date("2000-01-01");
console.log(Number(date)); // 946684800000

The difference between January 1, 2000, and January 2, 2000, is `86,400,000` milliseconds (1 day).

let date2 = new Date("2000-01-02");
console.log(Number(date2)); // 946771200000

The parseInt() Method

The parseInt() function converts a string into an integer. It ignores leading spaces and stops parsing at the first non-numeric character.

console.log(parseInt("-20")); // -20
console.log(parseInt("-20.99")); // -20
console.log(parseInt("50")); // 50
console.log(parseInt("50.99")); // 50
console.log(parseInt("50 years")); // 50
console.log(parseInt("years 50")); // NaN

The parseFloat() Method

The parseFloat() function converts a string into a floating-point number, stopping at non-numeric characters.

console.log(parseFloat("25")); // 25
console.log(parseFloat("25.75")); // 25.75
console.log(parseFloat("25 50")); // 25
console.log(parseFloat("25 years")); // 25
console.log(parseFloat("years 25")); // NaN

The Number.isInteger() Method

The Number.isInteger() method checks whether a value is an integer.

console.log(Number.isInteger(20)); // true
console.log(Number.isInteger(20.5)); // false

The Number.isSafeInteger() Method

A safe integer is a number that can be accurately represented in JavaScript without rounding errors. The Number.isSafeInteger() method checks if a number is within the safe integer range.

console.log(Number.isSafeInteger(1000000)); // true
console.log(Number.isSafeInteger(9007199254740992)); // false

The Number.parseFloat() Method

The Number.parseFloat() function converts a string into a floating-point number, similar to parseFloat().

console.log(Number.parseFloat("15.75")); // 15.75
console.log(Number.parseFloat("15 25")); // 15
console.log(Number.parseFloat("15 years")); // 15
console.log(Number.parseFloat("years 15")); // NaN

The Number.parseInt() Method

The Number.parseInt() function is equivalent to the parseInt() function and converts a string into an integer.

console.log(Number.parseInt("42")); // 42
console.log(Number.parseInt("42.99")); // 42
console.log(Number.parseInt("42 years")); // 42
console.log(Number.parseInt("years 42")); // NaN

Questions & Answers