Psyche Psyche - 12 days ago 7
PHP Question

How to make number_format() not to round numbers up

I have this number:

$double = '21.188624';


After using
number_format($double, 2, ',', ' ')
I get:

21,19


But what I want is:

21,18


Any ideea how can I make this work?

Thank you.

Answer

number_format will always do that, your only solution is to feed it something different:

$number = intval(($number*100))/100;

Or:

$number = floor(($number*100))/100;