UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

28 lines (27 loc) 799 B
/** * Round a value to the given number of decimal digits. If no decimal value is present, or if * `undefined` digits are given, no rounding occurs. * * @category Number * @category Package : @augment-vir/common * @example * * ```ts * import {round} from '@augment-vir/common'; * * // `result1` is `5.13` * const result1 = round(5.125, {digits: 2}); * // `result2` is `5` * const result2 = round(25, {digits: 2}); * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function round(value, { digits, }) { if (digits == undefined) { return value; } const digitFactor = Math.pow(10, digits); const multiplied = value * digitFactor; return Number((Math.round(multiplied) / digitFactor).toFixed(digits)); }