round()
round(
n,precision):number
Computes number rounded to precision.
DEPRECATED
Use Math.round() directly instead.
Parametersโ
n: numberโ
The number to round.
precision: number = 0โ
The precision to round to. Defaults to 0.
Returns: numberโ
The rounded number.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
round (Lodash, es-toolkit) ยท โ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleโ
// โ Deprecated approach
round(4.006); // => 4
round(4.006, 2); // => 4.01
round(4060, -2); // => 4100
// โ
Recommended approach
Math.round(4.006); // => 4
Math.round(4.006 * 100) / 100; // => 4.01
Math.round(4060 / 100) * 100; // => 4100
How it works?โ
Rounds number to specified precision.
Deprecated: Use Math.round() directly.
Native Equivalentโ
// โ round(value)
// โ
Math.round(value)
// โ
Math.round(value * 100) / 100 // for precision
Use Casesโ
Round numbers ๐โ
Round numbers to integers.
Math.ceil(4.3); // => 5
Math.floor(4.7); // => 4
Math.round(4.5); // => 5
Round to decimalsโ
Round to specific decimal places.
const round2 = (n) => Math.round(n * 100) / 100;
round2(4.567); // => 4.57
Pagination mathโ
Calculate page count.
const totalPages = Math.ceil(totalItems / pageSize);