Skip to main content

Archive

Show more

JavaScript Number Methods

JavaScript Number Methods

JavaScript provides a variety of methods for working with numbers, allowing you to perform mathematical operations, format numbers, and handle various numerical tasks. This article covers some of the key number methods and provides examples of how to use them effectively.


Common Number Methods

JavaScript's Number object provides several methods for numerical operations. Here are some commonly used number methods:

// Example number
const num = 123.456;

// Method: toFixed
console.log('toFixed Method:');
console.log(num.toFixed(2)); // Output: 123.46

// Method: toExponential
console.log('\ntoExponential Method:');
console.log(num.toExponential(2)); // Output: 1.23e+2

// Method: toPrecision
console.log('\ntoPrecision Method:');
console.log(num.toPrecision(5)); // Output: 123.46

// Method: toString
console.log('\ntoString Method:');
console.log(num.toString()); // Output: "123.456"

// Method: valueOf
console.log('\nvalueOf Method:');
console.log(num.valueOf()); // Output: 123.456

// Additional Methods

// Method: toLocaleString
console.log('\ntoLocaleString Method:');
console.log(num.toLocaleString('en-US', { style: 'currency', currency: 'USD' })); // Output: $123.46

// Method: Number.isInteger
console.log('\nNumber.isInteger Method:');
console.log(Number.isInteger(num)); // Output: false

// Method: Number.isFinite
console.log('\nNumber.isFinite Method:');
console.log(Number.isFinite(num)); // Output: true

// Method: Number.isNaN
console.log('\nNumber.isNaN Method:');
console.log(Number.isNaN(num)); // Output: false

// Method: Number.parseFloat
console.log('\nNumber.parseFloat Method:');
console.log(Number.parseFloat('123.456')); // Output: 123.456

// Method: Number.parseInt
console.log('\nNumber.parseInt Method:');
console.log(Number.parseInt('123.456', 10)); // Output: 123

Number Methods Explained

toFixed

The toFixed method formats a number using fixed-point notation. It takes one parameter, the number of decimal places to include.

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

toExponential

The toExponential method returns a string representing the number in exponential notation. It takes one parameter, the number of digits after the decimal point.

const value = 123456;
console.log(value.toExponential(3)); // Output: 1.235e+5

toPrecision

The toPrecision method formats a number to a specified length, returning a string representation of the number. It takes one parameter, the number of significant digits.

const value = 123.456789;
console.log(value.toPrecision(4)); // Output: 123.5

toString

The toString method converts a number to a string representation. You can also specify a radix (base) for the numeral system (e.g., binary, octal, hexadecimal).

const value = 255;
console.log(value.toString()); // Output: "255"
console.log(value.toString(16)); // Output: "ff" (hexadecimal representation)

valueOf

The valueOf method returns the primitive value of a number object. It is usually not necessary to call this method explicitly, as it is called automatically when a number is used in arithmetic operations.

const value = new Number(42);
console.log(value.valueOf()); // Output: 42

Conclusion

JavaScript number methods offer a range of functionalities for formatting, converting, and manipulating numerical values. By understanding and utilizing these methods, you can handle various numerical tasks more effectively and efficiently in your JavaScript applications. Whether you need to format numbers for display or convert them into different notations, these methods provide the tools you need to work with numbers in a versatile way.

Comments