@azizbecha/strkit
Version:
strkit is a utility library offering a collection of essential string functions including validation, case conversion, truncation, and more. Ideal for both JavaScript and TypeScript developers to simplify string operations in their applications.
28 lines • 1.01 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = roundTo;
/**
* Rounds a number to the specified number of decimal places.
* @param num - The number to round.
* @param decimalPlaces - The number of decimal places to round to. Defaults to 0.
* @returns The rounded number.
* @example
* roundTo(1.2345, 2); // 1.23
* roundTo(1.6789, 0); // 2
* roundTo(123.456, 1); // 123.5
*/
function roundTo(num, decimalPlaces = 0) {
const factor = 10 ** decimalPlaces;
return Math.round(num * factor) / factor;
}
});
//# sourceMappingURL=roundTo.js.map