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);