Skip to content

Core\BigInt::toDecimal

$bigInt->toDecimal(): decimal

Returns this number as a decimal.

A decimal is the type for money, and for any number where rounding is not allowed. It stores a value exactly, and it has a fixed size: it reaches from -79228162514264337593543950335 to 79228162514264337593543950335. A Core\BigInt has no size limit, so not every number fits a decimal.

toDecimal returns the decimal equal to this number. The result has no digits after the decimal point, and it is exact. When the number is outside the range above, toDecimal throws an ArithmeticError. It never rounds a number to make it fit.

Use this when a whole-number calculation has to continue as money arithmetic. Add the parts up as a Core\BigInt, where no total is ever too large. Then convert once, and divide or multiply as a decimal.

Return value

Returns decimal. A decimal equal to this value.

Errors

  • ArithmeticErrorThis value needs more than 96 bits. A decimal cannot be that large.

Examples

A number as a decimal

<?nvs
// Shows how to convert a `Core\BigInt` to a `decimal`.
// `toDecimal` returns the `decimal` equal to this number. This prints 42.
echo Core\BigInt::of(42)->toDecimal(), "\n";
// The sign is kept, and zero is zero. This prints -7 and then 0.
echo Core\BigInt::of(-7)->toDecimal(), "\n";
echo Core\BigInt::of(0)->toDecimal(), "\n";
// The result is a `decimal`, so arithmetic with it is exact. This prints
// 42.25.
decimal $value = Core\BigInt::of(42)->toDecimal();
echo $value + (0.25 as decimal), "\n";
// A `decimal` reaches much further than an `int` does. This prints
// 79228162514264337593543950335, the largest number it holds.
echo Core\BigInt::parse('79228162514264337593543950335')->toDecimal(), "\n";
Output
42
-7
0
42.25
79228162514264337593543950335

When the number does not fit

<?nvs
// Shows what `toDecimal` does with a number no `decimal` holds. The largest
// one is 79228162514264337593543950335.
var $largest = Core\BigInt::parse('79228162514264337593543950335');
// This number fits, so it is returned unchanged.
echo $largest->toDecimal(), "\n";
// The next number up does not. `toDecimal` throws an `ArithmeticError`, and
// the message names the method and the number it refused.
try {
echo $largest->plus(Core\BigInt::of(1))->toDecimal(), "\n";
} catch (ArithmeticError $tooWide) {
echo $tooWide->message, "\n";
}
// The `Core\BigInt` itself is unchanged and has no size limit, so `toString`
// still prints every digit of a number `toDecimal` refuses.
echo $largest->multipliedBy(Core\BigInt::of(1000))->toString(), "\n";
Output
79228162514264337593543950335
Core\BigInt::toDecimal(): 79228162514264337593543950336 is wider than a `decimal` holds
79228162514264337593543950335000

A total in cents as an amount

<?nvs
// Shows how a program adds up amounts in cents and then prints the total as an
// amount of money. The parts are added as a `Core\BigInt`, where no total is
// ever too large, and converted once at the end.
array<string> $cents = ['4500000000', '129900000000', '77000', '1'];
var $total = Core\BigInt::of(0);
foreach ($cents as string $part) {
$total = $total->plus(Core\BigInt::parse($part));
}
// A `decimal` divides exactly, so no cent is lost on the way. This prints
// 1344000770.01.
decimal $amount = $total->toDecimal();
echo $amount / 100, "\n";
Output
1344000770.01