JavaScript Number Methods
Number methods help you work with numbers, providing functionality for type conversion, formatting, parsing, and validation. These built-in methods make number manipulation convenient and powerful.
Number Methods
The toString() method converts a number to a string and returns it. You can optionally provide a radix (base) parameter to convert to different number systems: binary (base 2), octal (base 8), decimal (base 10), or hexadecimal (base 16). For example, (255).toString(16) returns "ff". The toString() method is useful when you need to concatenate numbers with strings or display numbers as text.
The toFixed() method formats a number with a specified number of decimal places and returns it as a string. It rounds the number if necessary. For example, (123.456).toFixed(2) returns "123.46". This method is invaluable for displaying prices, percentages, or any number where you need consistent decimal formatting. Note that it returns a string, so you may need to convert back to a number for calculations.
The toPrecision() method formats a number to a specified total length (significant digits) and returns it as a string. Unlike toFixed() which specifies decimal places, toPrecision() specifies total digits. For example, (123.456).toPrecision(4) returns "123.5". This method is useful for scientific notation or when you need a specific level of precision regardless of the number's magnitude.
The Number() function (not a method) converts other data types to numbers. It can parse strings (Number("123") returns 123), convert booleans (Number(true) returns 1), and handle various inputs. If conversion fails, it returns NaN. Number() is stricter than parseInt() or parseFloat()—it won't extract numbers from strings with non-numeric characters. Number("123px") returns NaN while parseInt("123px") returns 123.
The parseInt() function parses a string and returns an integer. It reads from left to right until it encounters a non-numeric character, returning the integer portion found. For example, parseInt("123.45") returns 123, and parseInt("123px") returns 123. You can provide an optional radix parameter to specify the number base. Always specify the radix to avoid unexpected results: parseInt("08", 10).
The parseFloat() function parses a string and returns a floating-point number. Like parseInt(), it reads from left to right but preserves decimal points. For example, parseFloat("123.45") returns 123.45, and parseFloat("123.45px") returns 123.45. It only parses base-10 numbers. If the string doesn't start with a number, parseFloat() returns NaN.
let num = 123.456;
// toString
let str = num.toString();
console.log(str); // "123.456"
// toFixed
console.log(num.toFixed(2)); // "123.46"
console.log(num.toFixed(0)); // "123"
// toPrecision
console.log(num.toPrecision(4)); // "123.5"
// Number conversion
console.log(Number("123")); // 123
console.log(Number("12.34")); // 12.34
console.log(Number("Hello")); // NaN
// parseInt
console.log(parseInt("123")); // 123
console.log(parseInt("123.45")); // 123
console.log(parseInt("123px")); // 123
// parseFloat
console.log(parseFloat("123.45")); // 123.45