HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Number Methods

These number methods can be used on all JavaScript numbers:

  • toFixed():
  • toPrecision()
  • toString()
  • parseFloat()
  • isNaN()
  • isFinite()
  • Number.isInteger()
  • Number.isNaN()
  • Number.parseFloat()
  • Number.parseInt()

toFixed():

Formats a number using fixed-point notation with a specified number of digits after the decimal point.

Example

const num = 3.14159;
console.log(num.toFixed(2)); // Output: "3.14"

toPrecision():

Formats a number to a specified precision (total number of significant digits).

Example

const num = 3.14159;
console.log(num.toPrecision(3)); // Output: "3.14"

toString():

Converts a number to a string, optionally specifying the radix (base) for numerical systems other than base 10.

Example

const num = 42;
console.log(num.toString()); // Output: "42"
console.log(num.toString(2)); // Output: "101010"

parseInt():

Parses a string and returns an integer. It takes an optional radix (base) parameter.

Example

console.log(parseInt("42")); // Output: 42
console.log(parseInt("1010", 2)); // Output: 10

parseFloat():

Parses a string and returns a floating-point number.

Example

console.log(parseFloat("3.14")); // Output: 3.14

isNaN():

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

Example

console.log(isNaN("Hello")); // Output: true

isFinite():

Checks if a value is a finite number (not NaN, Infinity, or -Infinity).

Example

console.log(isFinite(42)); // Output: true
console.log(isFinite(Infinity)); // Output: false

Number.isInteger():

Checks if a value is an integer.

Example

console.log(Number.isInteger(42)); // Output: true
console.log(Number.isInteger(3.14)); // Output: false

Number.isNaN():

Checks if a value is NaN.

Example

console.log(Number.isNaN("Hello")); // Output: false
console.log(Number.isNaN(NaN)); // Output: true

Number.parseFloat()):

Parses a string and returns a floating-point number.

Example

console.log(Number.parseFloat("3.14")); // Output: 3.14


Number.parseInt():

Parses a string and returns an integer.

Example

console.log(Number.parseInt("42")); // Output: 42
You can click on above box to edit the code and run again.

Output