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); // 4100Related Functions
add
Adds two numbers.
ceil
Computes number rounded up to precision.
divide
Divide two numbers.
floor
Computes number rounded down to precision.
max
Computes the maximum value of array.
maxBy
This method is like max except that it accepts iteratee which is invoked for each element in array to generate the criterion by which the value is ranked.