Lesson 36 +15 XP

Number Methods

Number Methods

Number methods help you format and convert numbers.

toFixed

Formats a number with a fixed number of decimals:

(3.14159).toFixed(2); // "3.14" (a string)

toString

Converts a number to a string:

(42).toString(); // "42"

toPrecision

Formats to a specified total length:

(123.456).toPrecision(4); // "123.5"

Number() vs string methods

  • Number("42") converts string to number.
  • (42).toString() converts number to string.

toLocaleString

Formats a number using locale conventions, like thousands separators:

(1234567).toLocaleString(); // "1,234,567"

isInteger

Checks if a value is a whole number:

Number.isInteger(4);   // true
Number.isInteger(4.5); // false

TL;DR

  • toFixed controls decimals.
  • toString converts to a string.
  • toPrecision sets total digits.
  • toLocaleString adds local formatting.
  • Number.isInteger checks whole numbers.