0
$number = 628.30 * 100;
print_r([
    'raw' => $number,
    'int' => (int)$number,
]);
// Array ( [raw] => 62830 [int] => 62829 )

What's the best way to avoid this?

Martin
  • 13
  • 5
  • 1
    It's not the multiplication with 100 that's the issue, it's the math with the float (`628.30`). See https://www.php.net/manual/en/language.types.float.php and https://stackoverflow.com/questions/3726721/php-floating-number-precision for more info. – WOUNDEDStevenJones Aug 17 '21 at 14:07
  • You can preprend `ini_set('precision', 20);` to have a hint of what's going on. Short answer is to round instead of truncating. Long answer is that this is how floating point maths work. – Álvaro González Aug 17 '21 at 14:08

0 Answers0