How to round a number to a given number of digits in JavaScript

In this Article we will go through how to round a number to a given number of digits only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);

#Example

round(1.234567, 3);     // 1.235
round(1.234567, 4);     // 1.2346