Rounds a number to a fixed number of decimal places.
Uses toFixed + parseFloat rather than Math.round(x * 10 ** n) / 10 ** n because it is consistently fast across JS engines and avoids most floating point drift for the magnitudes used in analytics.
toFixed
parseFloat
Math.round(x * 10 ** n) / 10 ** n
The number to round.
Decimal places to keep. Defaults to 4.
4
The rounded number.
roundTo(0.123456); // 0.1235roundTo(0.123456, 2); // 0.12 Copy
roundTo(0.123456); // 0.1235roundTo(0.123456, 2); // 0.12
Rounds a number to a fixed number of decimal places.
Uses
toFixed+parseFloatrather thanMath.round(x * 10 ** n) / 10 ** nbecause it is consistently fast across JS engines and avoids most floating point drift for the magnitudes used in analytics.