php - Float number always round up -


I have a float number, I want to round it like this:

  1.301 = & Gt; 1.31 1.309 = & gt; 1.31 1.3001 = & gt; 1.30 1.3009 = & gt; 1.30  

So far, I have tried this:

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

It is almost complete, but still not correct:

  1.3301 => 1.34 // It should be 1.33  

Is there any way to solve my problem? Thank you very much for any help.

Resolved Answer from Niet Dark Absol: $ number = ceil (floor ($ number * 1000) / 10) / 100;

Then, tks help for everyone :)

what you see He is walking down for three points, then rotates to two.

I am not excusing to know how it is understood.

  $ number = ceil (floor ($ number * 1000) / 10) / 100;  

To place two decimal places, add:

  $ number = number_format ($ number, 2); // OR: $ number = sprintf ("% .2f", $ number);  

But keep in mind that both of the results above are in the string , therefore only the output should be used.


Comments