everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
16 lines (15 loc) • 566 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.preciseRound = void 0;
/**
* Rounds a number to a given number of decimal places, avoiding floating point errors.
* @author @dailker
* @param {number} n - The number to round.
* @param {number} decimals - The number of decimal places.
* @returns {number} The rounded number.
*/
function preciseRound(n, decimals) {
const factor = Math.pow(10, decimals);
return Math.round((n + Number.EPSILON) * factor) / factor;
}
exports.preciseRound = preciseRound;