How to Round to 2 decimal places with trailing zeros in JavaScript
In this tutorial, we will discuss how to Round to 2 decimal places with trailing zeros in JavaScript
Round to 2 decimal places with trailing zeros
Let’s see an Example
To round to 2 decimal places we are going to use toFixed() method in JavaScript.
Now, we are going to create a function called trailing_zeros()
.
function trailing_zeros(number) { number = number.toFixed(2); console.log(number); }
Here, we used toFixed(2) because we need to round to 2 decimal places.
For example, 5.135624 can be round to two decimal places as 5.13, and 0.88035 can be round to two decimal places as 0.88.
Input:
var number = 112.3256; trailing_zeros(number);
Output:
112.32
Also read: Check if an Array Contains a Specific Value in JavaScript
Leave a Reply