Skip to content

Core\Math::round

Core\Math::round(
    float $n,
    {precision?: int, mode?: Core\RoundMode},
): float

Core\Math::round rounds a float to a number of decimal places.

The precision option says how many decimal places to keep. The default is 0, which gives a whole number. A negative precision rounds to tens, hundreds and so on, so -2 turns 1234.5 into 1200. The result is always a float.

The mode option decides what happens to a value exactly halfway between two results, such as 2.5. The default, Core\RoundMode::HalfUp, rounds it away from zero, so 2.5 becomes 3. HalfDown rounds it toward zero, so 2.5 becomes 2. HalfEven gives the even neighbour, and HalfOdd gives the odd one. Up and Down round every value away from or toward zero.

A float cannot store most decimal fractions exactly. 1.005 is stored as a number slightly below it, so with two decimal places it rounds down to 1. Use a decimal when the digits must be exact, such as for money.

The examples below show precision and mode, then tens and hundreds, then an average rating shown with one decimal place.

Parameters

  • $nfloatThe number to round.

Options

  • precisionintoptionalHow many decimal places to keep. The default is 0, which rounds to a whole number. -2 rounds to hundreds.
  • modeCore\RoundModeoptionalA Core\RoundMode case. The default is RoundMode::HalfUp, which rounds a half away from zero.

Return value

Returns float. The rounded float. NaN and the infinities are returned unchanged. $n is also returned unchanged when precision is above 22 or below -22, or when $n * 10 ** precision is 2 ** 53 or more.

Examples

Precision and mode

<?nvs
// Shows how `precision` and `mode` change the result of rounding.
// Two decimal places are kept. This prints 3.14.
echo Core\Math::round(3.14159, { precision: 2 }), "\n";
// A half rounds away from zero by default. This prints 3, then -3.
echo Core\Math::round(2.5), "\n";
echo Core\Math::round(-2.5), "\n";
// `HalfEven` rounds a half to the even neighbour. This prints 2.
echo Core\Math::round(2.5, { mode: Core\RoundMode::HalfEven }), "\n";
Output
3.14
3
-3
2

Tens and stored values

<?nvs
// Shows rounding to tens and hundreds, and a decimal fraction that a `float` cannot store exactly.
// A negative precision rounds to the left of the point. This prints 1200, then 1230.
echo Core\Math::round(1234.5, { precision: -2 }), "\n";
echo Core\Math::round(1234.5, { precision: -1 }), "\n";
// `Up` rounds every value away from zero. This prints 1300.
echo Core\Math::round(1201.0, { precision: -2, mode: Core\RoundMode::Up }), "\n";
// 1.005 is stored as a number slightly below 1.005, so it rounds down. This prints 1.
echo Core\Math::round(1.005, { precision: 2 }), "\n";
Output
1200
1230
1300
1

Average rating

<?nvs
// Shows how a shop shows the average star rating of a product with one decimal place.
class Rating
{
// Returns the average of the ratings, rounded to one decimal place.
public static function average(array<float> $stars): float
{
float $sum = 0.0;
float $count = 0.0;
foreach ($stars as float $star) {
$sum = $sum + $star;
$count = $count + 1.0;
}
return Core\Math::round($sum / $count, { precision: 1 });
}
}
// The averages are 4.2 and 4.666..., so this prints 4.2, then 4.7.
echo 'Lamp: ', Rating::average([5.0, 4.0, 4.0, 5.0, 3.0]), " stars\n";
echo 'Chair: ', Rating::average([5.0, 5.0, 4.0]), " stars\n";
Output
Lamp: 4.2 stars
Chair: 4.7 stars