JavaScript Number Methods

What Are Number Methods

JavaScript provides a rich set of Number methods to manipulate and format numbers. These methods allow developers to perform tasks like converting numbers to strings, formatting decimals and handling numeric precision.

Key JavaScript Number Methods

  1. toString()
  2. toFixed()
  3. toPrecision()
  4. valueOf()
  5. isInteger()
  6. isNaN()
  7. parseInt()
  8. parseFloat()

1. toString(): Converting a Number to a String

The toString() method converts a number into a string.

Syntax

number.toString([radix]);
  • radix (optional): Specifies the base of the numeral system (e.g., 2 for binary, 16 for hexadecimal).

Example

let num = 255;
console.log(num.toString()); // Output: "255"
console.log(num.toString(2)); // Output: "11111111" (binary representation)
console.log(num.toString(16)); // Output: "ff" (hexadecimal representation)

2. toFixed(): Formatting Decimals

The toFixed() method formats a number to a fixed number of decimal places.

Syntax

number.toFixed(digits);
  • digits: Specifies the number of decimal places.

Example

let num = 3.14159;
console.log(num.toFixed(2)); // Output: "3.14"
console.log(num.toFixed(4)); // Output: "3.1416" (rounded)

3. toPrecision(): Formatting Precision

The toPrecision() method formats a number to a specified total number of significant digits.

Syntax

number.toPrecision(precision);
  • precision: Specifies the total number of digits.

Example

let num = 123.456;
console.log(num.toPrecision(4)); // Output: "123.5"
console.log(num.toPrecision(6)); // Output: "123.456"

4. valueOf(): Retrieving the Primitive Value

The valueOf() method returns the primitive numeric value of a Number object.

Syntax

number.valueOf();

Example

let num = new Number(100);
console.log(num.valueOf()); // Output: 100

5. isInteger(): Checking If a Number Is an Integer

The isInteger() method determines whether a given value is an integer.

Syntax

Number.isInteger(value);

Example

console.log(Number.isInteger(10));    // Output: true
console.log(Number.isInteger(10.5)); // Output: false

6. isNaN(): Checking If a Value Is NaN

The isNaN() method checks whether a value is NaN (Not-a-Number).

Syntax

Number.isNaN(value);

Example

console.log(Number.isNaN(NaN));         // Output: true
console.log(Number.isNaN(123)); // Output: false
console.log(Number.isNaN("Hello")); // Output: false (use `isNaN()` global function for strings)

7. parseInt(): Converting a String to an Integer

The parseInt() method parses a string and returns an integer. It stops parsing when it encounters a non-numeric character.

Syntax

parseInt(string, [radix]);
  • string: The value to parse.
  • radix: The numeral system base (default: 10).

Example

console.log(parseInt("123"));      // Output: 123
console.log(parseInt("123abc")); // Output: 123
console.log(parseInt("0xFF", 16)); // Output: 255 (hexadecimal to decimal)

8. parseFloat(): Converting a String to a Float

The parseFloat() method parses a string and returns a floating-point number.

Syntax

parseFloat(string);

Example

console.log(parseFloat("123.45"));      // Output: 123.45
console.log(parseFloat("123.45abc")); // Output: 123.45
console.log(parseFloat("abc123.45")); // Output: NaN

Combining Methods

You can combine different methods to achieve complex number manipulations.

Example

let str = "123.456";
let num = parseFloat(str).toFixed(2);
console.log(num); // Output: "123.46"

Limitations of JavaScript Number Methods

Precision Issues: Floating-point arithmetic in JavaScript can lead to imprecise results.

console.log(0.1 + 0.2); // Output: 0.30000000000000004

Handling Large Numbers: For very large integers, consider using BigInt.

Leave a Comment