How to cut the number to hundredths, not rounding it?

echo number_format(0.65999, 2, '.', ''); // 0.66, Округлило echo "\n"; echo intval(0.65999 * 100) / 100; // 0.65, Не округлило echo "\n"; echo intval(0.29 * 100) / 100; // 0.28, Округлило 

I tried many options from the Internet, everywhere rounding was not on one number, so on the other. Is there some kind of a normal solution, or you can't do without your own function?

https://ideone.com/qm7YUE

    2 answers 2

    Use the BCMath library and its bcdiv function:

     echo bcdiv(0.65999, 1, 2); // 0.65 

    Well, and as an option, you can use preg_replace :

     echo preg_replace('/\.\d{2}\K.+/', '', 0.65999); // 0.65 
    • one
      bcdiv is a great feature, thanks a lot! Everything works great! - ModaL

    There are ceil and floor functions:
    printf('%05.2f', floor(0.65999*100)/100);

    cut off the fractional part after multiplying by one and divide

    • Your option does not round 0.65999, but rounds 0.29 - ModaL