Skip to content

Core\Decimal::ceil

Rounds a number up, to a whole number or to a chosen number of digits after the point.

Core\Decimal::ceil always moves towards the larger number. 2.1 becomes 3, and -2.9 becomes -2. The second argument says how many digits after the point to keep, and leaving it out gives a whole number. The answer is exact, because it is a decimal and not a floating-point number.

Reach for it when a part of something still costs a whole one. The pages a list needs, the boxes an order fills and a fee that is always rounded in the seller's favour are all this.

Good to know: Core\Decimal::floor is the same cut in the other direction, and Core\Decimal::truncate cuts towards zero. The three give the same answer for a positive number and part on a negative one.

Parameters

  • $valuedecimalThe value to cut.
  • $scaleuintdefault 0The number of digits after the decimal point, at most 28. Without it, the result has none. The result always has exactly this many, so a shorter value is padded with zeros.

Return value

Returns decimal. The smallest decimal with $scale digits that is not below $value. ceil(-1.5) is -1, and ceil(1.001, 2) is 1.01.

Errors

  • ArithmeticErrorWhen $scale is more than 28. Also when $value has too many digits before the point to keep that many digits after it.

Examples

Round up to a whole number

<?nvs
// Shows how to round a number up to a whole number. `Core\Decimal::ceil` always
// moves towards the larger number.
array<decimal> $values = [1.00, 1.08, 8.34, -1.08];
foreach ($values as decimal $value) {
// This prints 1, 2, 9 and -1. A negative number moves towards zero.
echo Core\Decimal::ceil($value), "\n";
}
// A box holds twelve items, so an order of one hundred fills nine boxes. The
// last box is only part full, and it is still a whole box. This prints 9.
decimal $boxes = Core\Decimal::divideRounded(100, 12, 4, Core\RoundMode::HalfUp);
echo Core\Decimal::ceil($boxes), "\n";
Output
1
2
9
-1
9

Keep digits after the point

<?nvs
// Shows how to round up and still keep some digits after the point. The second
// argument says how many digits the answer keeps.
decimal $fee = 12.3456;
// With no second argument the answer is a whole number. This prints 13.
echo Core\Decimal::ceil($fee), "\n";
// Two digits after the point, so the answer is the next whole cent up.
// This prints 12.35.
echo Core\Decimal::ceil($fee, 2), "\n";
// Three digits. This prints 12.346.
echo Core\Decimal::ceil($fee, 3), "\n";
// The answer always carries the number of digits you asked for, even when
// nothing had to move. This prints 12.3456 and 12.34560.
echo Core\Decimal::ceil($fee, 4), ' ', Core\Decimal::ceil($fee, 5), "\n";
Output
13
12.35
12.346
12.3456 12.34560

Never charge less than the cost

<?nvs
// Shows how to price items so a sale never loses money. What each item cost is
// rounded up to the next whole cent, so the price always covers it.
array<decimal> $costs = [3.8412, 0.0071, 19.9950];
decimal $charged = 0.00;
foreach ($costs as decimal $cost) {
decimal $price = Core\Decimal::ceil($cost, 2);
echo $cost, ' is charged as ', $price, "\n";
$charged = $charged + $price;
}
// This prints 23.86. `Core\Decimal::floor` would have charged 23.83.
echo 'total charged: ', $charged, "\n";
Output
3.8412 is charged as 3.85
0.0071 is charged as 0.01
19.9950 is charged as 20.00
total charged: 23.86