Skip to content

Core\Decimal::truncate

Cuts a number to fewer digits by dropping the ones it does not keep.

Core\Decimal::truncate never changes a digit it keeps. 12.39 stays 12.39 even when the next digit is a 9. 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 the extra digits do not matter: a percentage in a progress line, a measurement written into a column of fixed width, or a value you must always print at or below its real size.

Good to know: truncate always moves towards zero. On a positive number it agrees with Core\Decimal::floor, and on a negative one it agrees with Core\Decimal::ceil. So truncate(-2.7) is -2, while floor(-2.7) is -3.

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. $value without the digits after $scale places. truncate(-1.9) is -1, and truncate(1.999, 2) is 1.99.

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

Keep the digits you ask for

<?nvs
// Shows how to cut a number to fewer digits. `Core\Decimal::truncate` drops
// the digits it does not keep, and never changes the ones it keeps.
decimal $reading = 12.3999;
// Two digits are kept and the rest are dropped. This prints 12.39.
echo Core\Decimal::truncate($reading, 2), "\n";
// Without a second argument the answer is a whole number. This prints 12.
echo Core\Decimal::truncate($reading), "\n";
// The answer always has the number of digits you asked for. Asking for more
// digits than the value has adds zeros, so this prints 12.399900.
echo Core\Decimal::truncate($reading, 6), "\n";
Output
12.39
12
12.399900

A negative number moves towards zero

<?nvs
// Shows where `Core\Decimal::truncate` sends a negative number. It always
// moves towards zero, so -2.7 becomes -2.
array<decimal> $values = [2.7, 2.1, -2.1, -2.7];
// This prints 2, 2, -2 and -2, one on each line.
foreach ($values as decimal $value) {
echo Core\Decimal::truncate($value), "\n";
}
// `Core\Decimal::floor` moves towards the smaller number and
// `Core\Decimal::ceil` towards the larger one. For -2.7 this prints -3 and -2.
echo Core\Decimal::floor(-2.7), ' ', Core\Decimal::ceil(-2.7), "\n";
Output
2
2
-2
-2
-3 -2

Progress in a copied file

<?nvs
// Shows how to print how far a copy has come, as a percentage. The extra
// digits are dropped, so the line never says 100 before the copy is finished.
array<decimal> $copied = [3, 274, 1023];
decimal $chunks = 1024;
foreach ($copied as decimal $done) {
decimal $percent = $done * 100 / $chunks;
// This prints 0.2%, then 26.7%, then 99.9%.
echo Core\Decimal::truncate($percent, 1), "%\n";
}
Output
0.2%
26.7%
99.9%