JavaScript Number Methods
JavaScript provides a variety of built-in methods to perform operations on numbers. These methods help in rounding, converting, and manipulating numerical values with ease.
1. toFixed()
The toFixed()
method formats a number to a specified number of decimal places and returns the result as a string.
let num = 3.14159;
let formattedNum = num.toFixed(2); // "3.14"
console.log(formattedNum);
In this example, the toFixed(2)
method rounds the number 3.14159
to two decimal places, resulting in "3.14"
.
2. toString()
The toString()
method converts a number to a string. This method can also accept an optional parameter to specify the base (radix) of the number.
let num = 255;
let binaryStr = num.toString(2); // "11111111"
let hexStr = num.toString(16); // "ff"
console.log(binaryStr, hexStr);
Here, num.toString(2)
converts the number 255
to its binary representation "11111111"
, and num.toString(16)
converts it to its hexadecimal representation "ff"
.
3. parseInt()
The parseInt()
function parses a string and returns an integer based on the specified base (radix).
let str = "100";
let intValue = parseInt(str, 10); // 100
let hexValue = parseInt("ff", 16); // 255
console.log(intValue, hexValue);
In this example, parseInt("100", 10)
converts the string "100"
to the integer 100
, while parseInt("ff", 16)
converts the hexadecimal string "ff"
to the integer 255
.
4. parseFloat()
The parseFloat()
function parses a string and returns a floating-point number.
let str = "3.14";
let floatValue = parseFloat(str); // 3.14
console.log(floatValue);
Here, parseFloat("3.14")
converts the string "3.14"
to the floating-point number 3.14
.
5. toPrecision()
The toPrecision()
method formats a number to a specified length and returns the result as a string.
let num = 3.14159;
let preciseNum = num.toPrecision(3); // "3.14"
console.log(preciseNum);
In this example, toPrecision(3)
formats the number 3.14159
to a string with three significant digits, resulting in "3.14"
.
6. isNaN()
The isNaN()
function determines whether a value is NaN
(Not-a-Number).
let num1 = NaN;
let num2 = 42;
console.log(isNaN(num1)); // true
console.log(isNaN(num2)); // false
In this example, isNaN(num1)
returns true
because num1
is NaN
, while isNaN(num2)
returns false
because num2
is a valid number.
7. Number.isInteger()
The Number.isInteger()
method determines whether a value is an integer.
let num1 = 42;
let num2 = 3.14;
console.log(Number.isInteger(num1)); // true
console.log(Number.isInteger(num2)); // false
Here, Number.isInteger(num1)
returns true
because num1
is an integer, while Number.isInteger(num2)
returns false
because num2
is a floating-point number.
8. Math.random()
The Math.random()
method generates a random floating-point number between 0
(inclusive) and 1
(exclusive).
let randomNum = Math.random();
console.log(randomNum); // A random number between 0 and 1
In this example, Math.random()
generates a random number between 0
and 1
.
9. Math.floor()
The Math.floor()
method rounds a number downward to the nearest integer.
let num = 4.7;
let floorNum = Math.floor(num); // 4
console.log(floorNum);
Here, Math.floor(4.7)
rounds the number 4.7
down to 4
.
10. Math.ceil()
The Math.ceil()
method rounds a number upward to the nearest integer.
let num = 4.2;
let ceilNum = Math.ceil(num); // 5
console.log(ceilNum);
In this example, Math.ceil(4.2)
rounds the number 4.2
up to 5
.
Conclusion
JavaScript provides a wide array of methods to work with numbers, allowing you to format, convert, and manipulate numerical values effectively. Understanding and utilizing these methods will enable you to handle numbers in your JavaScript projects more efficiently.
Comments
Post a Comment