Users can round a number to a maximum of 2 decimal places using five different methods in JavaScript. These are using .toFixed() method, Math.round() and parseFloat() functions and double rounding (.toPrecision()) and custom function.
This article will depict all these five methods of rounding a decimal number to two decimal places.
Method 1: Round a number to two decimal places using the Math.round() function in JavaScript:
What is Math.round()?
In JavaScript, Math.round() is a function that round the number input as a parameter to its nearest integer.
Syntax:
Math.round(number)
Parameters:
The function rounds the number to its nearest integer with two decimal places.
Code Snippet:
var numb= 8374848959.4863318274862938;
var rounded = Math.round((numb + Number.EPSILON) * 100) / 100;
console.log(rounded);
Explanation:
In the above code snippet, we have taken a long floating-point number as input and added a small number compared to that input number. Then, we will use the Number.EPSILON to provide the number's precise rounding.
We then multiply that number with 100 before rounding to fetch only the two digits after their decimal place. Lastly, we divide the number by 100 to get a maximum of two decimal places.
Output:
Browsers that support the Math.round() function are as follows:
- Internet Explorer
- Chrome
- Safari
- Firefox
- Edge
- Opera
Method 2: Round a number to two decimal places using the toFixed() method in JavaScript:
What is .toFixed()?
In JavaScript, toFixed() is a method. Users can use this method to format a number input applying fixed-point notation. Also, users can use this function to round a floating-point number to the nearest integer having a maximum of two decimal places.
It formats the number input with a particular number of digits to the right place of the decimal.
Syntax:
number.toFixed(value)
This method has one parameter.
Value
This parameter indicates the number of digits that will appear in the right place of the decimal.
Code Snippet:
var numb = 349875948759348.30374213;
numb = numb.toFixed(2);
console.log (numb)
Explanation:
In the above code sample, we have used the .toFixed() method on a number input and passed the number of digits to the right place of the decimal as an argument.
Output:
Limitation of this function:
This .toFixed() method does not generate exact outputs in some cases. There are many other better methods than this. If a number rounds of 2.2, it will yield a result of 2.20. If users insert a number like 5.005 as input, it will return 5.000 instead of 5.01.
Note:
Although the Math.round() improves over .toFixed(), it is still not the most reasonable solution and will also not round 5.005 accurately.
Browsers that supports the .toFixed() function:
- Internet Explorer
- Google Chrome
- Firefox
- Microsoft Edge
- Opera
- Apple Safari
Method 3: Round a number to two decimal places using the parseFloat() with toFixed() method in JavaScript:
What is parseFloat()?
Similar to the above JavaScript functions, the parseFloat() is a function that accepts a string input and converts it into a floating-point number. If the string input does not have a numeral value or the string's first character is not a numeral value, it returns NaN, i.e., not a number.
Syntax:
parseFloat(value)
This function has one parameter.
Value:
This parameter accepts a string. It converts the string input into an integer or a floating-point number (based on the string data type).
Code Snippet:
demo = parseFloat("4.349948").toFixed(2)
console.log (demo);
Explanation:
In the above code snippet, we have used both the parseFloat() and the .toFixed() method to round a number with two decimal places. We have given a string with a number value as an input.
The parseFloat() function first treats the input as a number, and the .toFixed() method converts the number into two decimal place floating-point numbers.
Output:
Method 4: Round a number to two decimal places using the .toPrecision() method in JavaScript:
What is .toPrecision()?
Like .toFixed() method, users can also use the .toPrecision() method in Javascript to format a number to a precise length or precision. If the input number they will format requires more digits than the original number, the interpreter will add decimals and nulls to create the specified precision.
Syntax:
number.toPrecision(value)
This function has one parameter.
Value:
The parameter defines the value of the number of significant digits a user wants to apply to the formatted number.
Code Snippet:
function round(num) {
var m = Number( (Math.abs(num) * 1000).toPrecision(12) );
return Math.round(m) / 100 * Math.sign(num);
}
console.log(round(2.008));
Explanation:
In this method, we have used the .toPrecision() method to cut off the floating-point rounding errors raised during the intermediate calculations in single rounds of floating-point numbers.
Output:
Method 5: Round a number to two decimal places using the custom functions in JavaScript:
Code Snippet:
function roundToTwo(num) {
return + (Math.round(num + "e+3") + "e-2");
}
console.log(roundToTwo(3.394792));
Explanation:
In the example, we have used the Math.round() with roundToTwo(num) to shift and convert the input number into two decimal places. The custom function takes care of every corner point while rounding the decimals such as 3.394792 is calculated well by this function.
Output:
Conclusion:
To round a number floating-point number to two decimal places, programmers can use any of the methods mentioned above to improve their code readability and performance. All these methods are efficient ways to round the floating point values.
Programmers should understand that when they use the method, it precisely rounds the number to two decimal places.