@monstermann/fn
Version:
A utility library for TypeScript.
27 lines (25 loc) • 679 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/number/limitPrecision.ts
/**
* `limitPrecision(target, precision)`
*
* Rounds `target` to the specified number of decimal places defined by `precision`.
*
* ```ts
* limitPrecision(3.14159, 2); // 3.14
* limitPrecision(2.7182818, 3); // 2.718
* limitPrecision(123.456, 0); // 123
* ```
*
* ```ts
* pipe(3.14159, limitPrecision(2)); // 3.14
* pipe(2.7182818, limitPrecision(3)); // 2.718
* pipe(123.456, limitPrecision(0)); // 123
* ```
*/
const limitPrecision = dfdlT((target, precision) => {
precision = 10 ** precision;
return Math.round(target * precision) / precision;
}, 2);
//#endregion
export { limitPrecision };