Javascript Fixed Number of Digits After Decimal Places
Hello dev's, today I'll show you how to fixed the number of digits after the decimal places. We'll use JavaScript in built method toFixed(). Using toFixed(), we can't only fixed the number of digits, we can round numbers, convert the number into string etc. So, let's see the usages toFixed() function.
Usage of toFixed()
Let's see the below code snippet
console.log(1.1.toFixed(2));
console.log(1.15.toFixed());
console.log(2.12.toFixed(2));
console.log(3.123.toFixed(2));
console.log(4.125.toFixed(2));
console.log(5.125.toFixed(4));
In the above example, we can see that we can easily round up or fixed the number. It'll produce the below output:
That's it for today. I hope you've enjoyed this tutorial. Thanks for reading. ๐