Search code examples
phpdecimalroundingnumber-formattingdecimal-point

number_format is rounding final decimal numbers and increasing to next round decimal number


I have a DOUBLE data type column in my MySQL table and I have a value of:

14.5299

But PHP number_format rounds that value to:

14.5300 

And if I just echo 14.5299, it outputs:

14.53

How can I output 14.5299 without hardcoding anything in my HTML markup?


Solution

  • Use sprintf()

    echo sprintf("%2.4f", 14.5399);
    

    Documentation: https://www.php.net/sprintf