Float with PHP precision

Asked

Viewed 390 times

10

I am bringing from the database the sum of the values and as a result it returns me as the example below:

6.3285714285714

I wish he’d stay that way:

6.32

I’ve tried the ceil() and the round(), but both returned me for more or less. I tried to use the substr($valor,0,4);, but the problem is when the value is:

6.328

Remembering that the value can also return:

16.3285714285714

And I believe that the substr() would not be the solution. How could I solve this?

  • 1

    try this way substar($value+0,4);

  • Thanks Willian, but Maniero’s solution was the one that best adapted.

  • 1

    Excellent, learning for me too! haha

2 answers

9


  • 1

    Perfect Maniero. It worked!! Thank you very much!

  • Once again saving me, thank you Maniero!

0

I guess in this case I wouldn’t have to round up?

$valor = 6.3285714285714;
echo number_format(floatval($valor), 2, '.', '');

Now if you don’t want to round it up:

echo number_format(floor(($valor*100))/100, 2, '.', '');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.