Home > Web Front-end > JS Tutorial > Mastering Number Methods in JavaScript

Mastering Number Methods in JavaScript

Barbara Streisand
Release: 2024-12-21 12:27:14
Original
1002 people have browsed it

Mastering Number Methods in JavaScript

Number Methods in JavaScript

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.


1. Converting Numbers

A. toString()

Converts a number to a string.

const num = 123;
console.log(num.toString()); // Output: "123"
console.log((456).toString()); // Output: "456"
Copy after login
Copy after login

B. toFixed(digits)

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"
Copy after login
Copy after login

C. toExponential(digits)

Returns the number in exponential notation.

const num = 12345;
console.log(num.toExponential(2)); // Output: "1.23e+4"
Copy after login
Copy after login

D. toPrecision(digits)

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"
Copy after login
Copy after login

2. Parsing and Validating Numbers

A. parseInt(string, radix)

Parses a string into an integer.

console.log(parseInt("123")); // Output: 123
console.log(parseInt("101", 2)); // Output: 5 (binary to decimal)
Copy after login

B. parseFloat(string)

Parses a string into a floating-point number.

console.log(parseFloat("3.14")); // Output: 3.14
console.log(parseFloat("123abc")); // Output: 123
Copy after login

C. Number.isInteger(value)

Checks if a value is an integer.

console.log(Number.isInteger(123)); // Output: true
console.log(Number.isInteger(3.14)); // Output: false
Copy after login

D. Number.isFinite(value)

Checks if a value is a finite number.

console.log(Number.isFinite(123)); // Output: true
console.log(Number.isFinite(Infinity)); // Output: false
Copy after login

E. Number.isNaN(value)

Checks if a value is NaN (Not-a-Number).

console.log(Number.isNaN(NaN)); // Output: true
console.log(Number.isNaN(123)); // Output: false
Copy after login

3. Rounding Numbers

A. Math.round()

Rounds a number to the nearest integer.

console.log(Math.round(4.5)); // Output: 5
console.log(Math.round(4.4)); // Output: 4
Copy after login

B. Math.ceil()

Rounds a number up to the next integer.

console.log(Math.ceil(4.1)); // Output: 5
Copy after login

C. Math.floor()

Rounds a number down to the previous integer.

console.log(Math.floor(4.9)); // Output: 4
Copy after login

D. Math.trunc()

Removes the decimal part of a number.

console.log(Math.trunc(4.9)); // Output: 4
Copy after login

4. Generating Random Numbers

A. Math.random()

Generates a random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // Output: A random number between 0 and 1
Copy after login

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
Copy after login

5. Other Useful Number Methods

A. Math.abs()

Returns the absolute value of a number.

console.log(Math.abs(-5)); // Output: 5
Copy after login

B. Math.pow(base, exponent)

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"
Copy after login
Copy after login

C. Math.sqrt()

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"
Copy after login
Copy after login

D. Math.max() and Math.min()

Finds the maximum or minimum value among a set of numbers.

const num = 12345;
console.log(num.toExponential(2)); // Output: "1.23e+4"
Copy after login
Copy after login

E. Math.sign()

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"
Copy after login
Copy after login

6. Summary

  • JavaScript provides a wide range of methods to work with numbers, from conversion to complex mathematical operations.
  • The Math object contains utility functions for advanced calculations.
  • Use these methods to handle rounding, parsing, formatting, and generating numbers efficiently.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template