JavaScript Number Methods
Essential methods for working with numbers in JavaScript
🔢 What are Number Methods?
JavaScript Number methods are built-in functions that help you work with numbers. They can format, convert, and manipulate numeric values easily.
// Simple number method example
let num = 123.456;
console.log(num.toFixed(2)); // "123.46"
Output:
123.46
Common Number Methods
toFixed()
Round to specific decimal places
let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
toPrecision()
Format to specific length
let num = 123.456;
console.log(num.toPrecision(4)); // "123.5"
toString()
Convert number to string
let num = 42;
console.log(num.toString()); // "42"
toExponential()
Scientific notation format
let num = 1234;
console.log(num.toExponential(2)); // "1.23e+3"
🔹 toFixed() Method
The toFixed() method formats a number with a fixed number of decimal places:
let price = 19.99567;
// Round to 2 decimal places (common for money)
console.log(price.toFixed(2)); // "20.00"
// Round to 0 decimal places (whole number)
console.log(price.toFixed(0)); // "20"
// Round to 4 decimal places
console.log(price.toFixed(4)); // "19.9957"
Output:
20.00
20
19.9957
🔹 toPrecision() Method
The toPrecision() method formats a number to a specified total length:
let num = 123.456;
// 3 significant digits
console.log(num.toPrecision(3)); // "123"
// 5 significant digits
console.log(num.toPrecision(5)); // "123.46"
// 2 significant digits
console.log(num.toPrecision(2)); // "1.2e+2"
Output:
123
123.46
1.2e+2
🔹 toString() Method
The toString() method converts a number to a string, with optional base conversion:
let num = 255;
// Convert to string (base 10)
console.log(num.toString()); // "255"
// Convert to binary (base 2)
console.log(num.toString(2)); // "11111111"
// Convert to hexadecimal (base 16)
console.log(num.toString(16)); // "ff"
// Convert to octal (base 8)
console.log(num.toString(8)); // "377"
Output:
255
11111111
ff
377
🔹 valueOf() Method
The valueOf() method returns the primitive value of a number:
let num = new Number(42);
console.log(num.valueOf()); // 42
console.log(typeof num.valueOf()); // "number"
// Usually not needed, but useful for object numbers
let result = num.valueOf() + 8;
console.log(result); // 50
Output:
42
number
50
🔹 Practical Examples
Real-world usage of number methods:
// Shopping cart total
let subtotal = 29.99;
let tax = 2.4567;
let total = subtotal + tax;
console.log("Subtotal: $" + subtotal.toFixed(2)); // "Subtotal: $29.99"
console.log("Tax: $" + tax.toFixed(2)); // "Tax: $2.46"
console.log("Total: $" + total.toFixed(2)); // "Total: $32.45"
// Scientific calculations
let distance = 149597870.7; // km to sun
console.log("Distance: " + distance.toExponential(2) + " km"); // "Distance: 1.50e+8 km"
// Color conversion
let red = 255;
let green = 128;
let blue = 0;
console.log("Hex color: #" + red.toString(16) + green.toString(16) + blue.toString(16)); // "Hex color: #ff800"
Output:
Subtotal: $29.99
Tax: $2.46
Total: $32.45
Distance: 1.50e+8 km
Hex color: #ff800