Skip to content

Tag: floating-point

PHP Printf As Float Precision

I am attempting to use PHP’s printf function to print out a user’s storage capacity. The full formula looks something like this: Given that $size == (10 * 1024 * 1024), this should print out 10.00 GB But it doesn’t. It prints 10.04 GB. Furthermore, results in 10.04 What?! In giving it an integer to convert to a float, it

PHP unexpected result of float to int type cast

I’trying to convert a float to an int value in php: I can use ceil to make it work but can somebody explain this to me? Answer This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses. See >php -r “echo var_dump(sprintf(‘%.40F’, 39.3