2

I have a code where I got some numbers like this:

92.682926829268 

I'd like to cut them like this:

92.68

This is my code:

<td><?php if (($row['TotalMatch']) > 10){ echo ($row['OK_05'] / $row['TotalMatch']) * 100; }   ?></td>

I tried with floor and round but I get that example I showed at the beginning of post ( 92.682926829268 instead of 92.68 )

Thanks for your attention

Regards!

EDIT Could you give me an example with my code? Thanks

6
  • 6
    round(92.682926829268, 2); should be your friend
    – B001ᛦ
    Feb 14, 2017 at 17:18
  • 1
    but I don't get what I want - Please elaborate. We can't read your mind.
    – Mike
    Feb 14, 2017 at 17:19
  • 2
    Yes, what do you get? The code in your question doesn't include floor or round. If you can show what you did with those functions, someone can tell you why it didn't get you the result you wanted. Feb 14, 2017 at 17:32
  • I edited in bold and added an edit. I put at the begin what I wanted to get :)
    – James69
    Feb 14, 2017 at 17:41
  • Your edit does not show what you did with floor or round to get that number. Feb 14, 2017 at 17:46

5 Answers 5

6

Use sprintf() to format the number.

echo sprintf("%.2f", 92.682926829268);

Example: https://3v4l.org/U87T9

1
  • Could you show me in my example, please? because I don't know how I can insert it in my code. Thanks
    – James69
    Feb 14, 2017 at 17:42
4

The expression you're trying to format is this:

($row['OK_05'] / $row['TotalMatch']) * 100

So whichever function you decide to use needs to go around that expression.

As to which function to use, you need to select one that returns a string, not a float.

If you use round, and your expression returns a float that rounds to a number with two zeros after the decimal point, the trailing zeros will not be displayed in the result. For example, echo round(92.0006829268, 2) will display 92, not 92.00. So don't use round if you need to be sure that two decimal places are always displayed. round is a math function, not a formatting function.

floor is really not useful at all here, as it returns a number with no decimal places.


A simple way is to use sprintf as shown in some of the other answers.

echo sprintf("%.2f", ($row['OK_05'] / $row['TotalMatch']) * 100);

The first argument to sprintf is "%.2f", which is a format string indicating that the second argument should be displayed as a float with two decimal places. The second argument is your expression.

Using bcdiv as suggested in the other answer will also work, but it works a little differently that sprintf and will produce a slightly different result in some cases.

sprintf will round to the number of decimal places specified, so for example

echo sprintf("%.2f", 926.89 / 10);   // outputs 92.69

and bcdiv will truncate instead, so

echo bcdiv(926.89, 10, 2);           // outputs 92.68

Whichever one of those works for you, do that.

1
  • It's perfect, Thanks!
    – James69
    Feb 14, 2017 at 19:16
2

You can use the round function

$var = 92.682926829268;
$var = round($var, 2)

Or use sprintf (%.2f cuts the number)

$var = sprintf("%.2f", $var);
0

Try using sprintf like below:

<?php
$mynumber = 98.343434;
echo sprintf('%.2f', $mynumber);    // this will output 98.34
0

You could use bcdiv()

bcdiv($row['OK_05'], ($row['TotalMatch'] * 100), 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.