@artcore. foolproof for my purposes intval(strval('15.20 ' * 100))
Sep 30, 2020
34
Level 122
Level 102
@snapey You should never be sorry for changing Best answer.
If your own answer is the best, then it is best to mark that. Will make it easier for others with the same issue, to find the best solution.
Level 56
Sorry for bumping this up, but I just wanted to share how I handled this on a project.
If you have the bcmath extension available you can use bcmul
https://www.php.net/manual/en/function.bcmul.php
$ php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.10 — cli) by Justin Hileman
>>> intval(bcmul('8.03', 100.0, 2))
=> 803
>>> intval(bcmul('150.2', 100.0, 2))
=> 15020
>>> intval(bcmul('19.99', 100.0, 2))
=> 1999
Which can be paired with bcdiv when converting back:
$ php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.10 — cli) by Justin Hileman
>>> floatval(bcdiv(803, 100.0, 2))
=> 8.03
>>> floatval(bcdiv(10520, 100.0, 2))
=> 105.2
>>> floatval(bcdiv(1999, 100.0, 2))
=> 19.99
I ended up using this because for a particular project I needed 6-digits decimal precision. I am actually not aware if using the intval/strval pair would work on that project, so thanks for sharing.
1 like
Please or to participate in this conversation.