How do I convert a number to a big decimal in PHP?
For example, I have a number: 788892,667
.
I want to convert it to a big decimal: 788892667
,
then convert it back to a long 788892,667
.
Could I do this in PHP?
How do I convert a number to a big decimal in PHP?
For example, I have a number: 788892,667
.
I want to convert it to a big decimal: 788892667
,
then convert it back to a long 788892,667
.
Could I do this in PHP?
PHP has no internal type for representing big numbers. What it does have is extensions to deal with such numbers: GMP and BCMath, but they are quite low-level.
If you want a library that provides BigDecimal
and BigInteger
, try brick/math (disclaimer: I'm the author):
use Brick\Math\BigDecimal;
$bigdecimal = BigDecimal::of('788892.667'); // BigDecimal(788892.667)
$biginteger = $bigdecimal->getUnscaledValue(); // BigInteger(788892667)
$int = $biginteger->toInt(); // int(788892667)
This library makes use of the GMP and BCMath extensions when available, but also works without them, so it should be a good fit for any PHP project.