Aller au contenu principal

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

Math.round() - MDN


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