Oftentimes, we’ve to round floating-point numbers in our JavaScript web apps.
In this article, we’ll look at how to round floating-point numbers in JavaScript.
JavaScript numbers have the toFixed
method that returns a string version of the number rounded to the number of decimal places we passed in as the argument.
For instance, we can write:
const rounded = Number((6.6756854).toFixed(1));
console.log(rounded)
Then rounded
is '6.7'
since we passed in 1 to toFixed
.
This makes it returns a string rounded to 1 decimal place.
#javascript