Back to Functions

round

math

Computes number rounded to precision.

Installation

Import
import { round } from '@tulx/utils';

Source Code

Implementation
/**
 * Computes number rounded to precision.
 *
 * @param number - The number to round.
 * @param precision - The precision to round to.
 * @returns Returns the rounded number.
 *
 * @example
 * ```ts
 * round(4.006); // 4
 * round(4.006, 2); // 4.01
 * round(4060, -2); // 4100
 * ```
 */
export function round(number: number, precision: number = 0): number {
  const factor = Math.pow(10, precision);
  return Math.round(number * factor) / factor;
}

Example

import { round } from '@tulx/utils';

round(4.006); // 4
round(4.006, 2); // 4.01
round(4060, -2); // 4100

Related Functions