Restrict a float to two decimal points in JavaScript
This post will discuss how to restrict a float to two decimal points in JavaScript.
There are several methods to restrict a float to two decimal points in JavaScript. A float is a number that has a fractional part, such as 2.25 or 3.14. Restricting a float to two decimal points means formatting the number to have only two digits after the decimal point, rounding if necessary. For example, 3.1415 should be restricted to 3.14. Here are some of the methods that we can use:
1. Using Number.toFixed() function
To restrict a float to two decimal points in JavaScript, we can use the built-in function Number.toFixed(). It takes a number of digits as an argument and returns a string representation of the number that is formatted to a fixed number of decimal places. For example:
|
1 2 3 4 5 6 |
let num = 3.14159; // Format the float to two decimal places using the toFixed function let formattedNum = num.toFixed(2); console.log(formattedNum); // '3.14' |
Note that the Number.toFixed() function returns a string, not a number. If we want to convert the string back to a number, we can use the parseFloat() function or the unary plus operator (+). For example:
|
1 2 3 4 5 6 7 |
let num = 3.1415; let num1 = parseFloat(num.toFixed(2)); console.log(num1); // '3.14' let num2 = +num.toFixed(2); console.log(num2); // '3.14' |
However, be aware that the toFixed() function may not always round the number as we expect, due to the limitations of floating-point arithmetic. For example:
|
1 2 3 4 |
let num = 1.005; // "1.00", not "1.01" console.log(num.toFixed(2)); |
This is because the number 1.005 cannot be represented exactly in binary, and is actually stored as something slightly less than the number 1.005, which causes it to round down instead of up.
2. Using Math.round() function
The Math.round() function is a built-in function that takes a number as an argument and returns the value of the number rounded to the nearest integer. We can use this function to restrict a float to two decimal points by multiplying it by 100, rounding it to the nearest integer, and dividing it by 100. For example:
|
1 2 3 4 5 6 |
let num = 3.1415; // Multiply the float by 100, round it, and divide it by 100 let roundedNum = Math.round(num * 100) / 100; console.log(roundedNum); // 3.14 |
3. Using regular expression
We can use a regular expression to match the pattern of a float with two or more digits after the decimal point and replace it with only two digits using the replace() function. For example, the following function takes a float as an argument and returns a string representation of the number restricted to two decimal points using a regular expression:
|
1 2 3 4 5 6 |
function restrictFloat(num) { // Replace the matched pattern with only two digits using the replace() function return String(num).replace(/(\.\d\d)\d+/, "$1"); } console.log(restrictFloat(3.1415)); // "3.14" |
4. Using Number.toPrecision() function
The Number.toPrecision() function of the Number object takes a number of significant digits as an argument and returns a string representation of the number formatted to that number of significant digits. We can use this function to restrict a float to two decimal points, but it may use exponential notation for very large or very small numbers. For example:
|
1 2 3 4 5 6 |
let num = 3.14159; // Format the float to two decimal places using the toPrecision function let formattedNum = num.toPrecision(3); console.log(formattedNum); // '3.14' |
That’s all about restricting a float to two decimal points in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)