JavaScript provides several built-in methods to work with numbers effectively. These methods allow you to perform operations like formatting, rounding, parsing, and validating numbers.
Converts a number to a string.
const num = 123; console.log(num.toString()); // Output: "123" console.log((456).toString()); // Output: "456"
Formats a number with a fixed number of decimal places.
const num = 3.14159; console.log(num.toFixed(2)); // Output: "3.14" console.log(num.toFixed(4)); // Output: "3.1416"
Returns the number in exponential notation.
const num = 12345; console.log(num.toExponential(2)); // Output: "1.23e+4"
Formats a number to a specified total length (including decimals).
const num = 123.456; console.log(num.toPrecision(4)); // Output: "123.5" console.log(num.toPrecision(6)); // Output: "123.456"
Parses a string into an integer.
console.log(parseInt("123")); // Output: 123 console.log(parseInt("101", 2)); // Output: 5 (binary to decimal)
Parses a string into a floating-point number.
console.log(parseFloat("3.14")); // Output: 3.14 console.log(parseFloat("123abc")); // Output: 123
Checks if a value is an integer.
console.log(Number.isInteger(123)); // Output: true console.log(Number.isInteger(3.14)); // Output: false
Checks if a value is a finite number.
console.log(Number.isFinite(123)); // Output: true console.log(Number.isFinite(Infinity)); // Output: false
Checks if a value is NaN (Not-a-Number).
console.log(Number.isNaN(NaN)); // Output: true console.log(Number.isNaN(123)); // Output: false
Rounds a number to the nearest integer.
console.log(Math.round(4.5)); // Output: 5 console.log(Math.round(4.4)); // Output: 4
Rounds a number up to the next integer.
console.log(Math.ceil(4.1)); // Output: 5
Rounds a number down to the previous integer.
console.log(Math.floor(4.9)); // Output: 4
Removes the decimal part of a number.
console.log(Math.trunc(4.9)); // Output: 4
Generates a random number between 0 (inclusive) and 1 (exclusive).
console.log(Math.random()); // Output: A random number between 0 and 1
To generate a random number within a range:
const min = 1; const max = 10; const random = Math.floor(Math.random() * (max - min + 1)) + min; console.log(random); // Output: A random number between 1 and 10
Returns the absolute value of a number.
console.log(Math.abs(-5)); // Output: 5
Returns the base raised to the power of the exponent.
const num = 123; console.log(num.toString()); // Output: "123" console.log((456).toString()); // Output: "456"
Returns the square root of a number.
const num = 3.14159; console.log(num.toFixed(2)); // Output: "3.14" console.log(num.toFixed(4)); // Output: "3.1416"
Finds the maximum or minimum value among a set of numbers.
const num = 12345; console.log(num.toExponential(2)); // Output: "1.23e+4"
Returns the sign of a number (-1, 0, or 1).
const num = 123.456; console.log(num.toPrecision(4)); // Output: "123.5" console.log(num.toPrecision(6)); // Output: "123.456"
By mastering these number methods, you can handle various numerical operations in JavaScript with ease.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
The above is the detailed content of Mastering Number Methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!