PHP Simple Division Problem

Hi All,

I'm embarrassed to admit it, however, desperation leads to desperate measures...

I have a simple decimal division which is working for some parts however, when I get to the last part of the calculation with the final division, the result I get isn't what I'm trying to achieve.  My overall calculation is:
$a = 1110651.36;
$b = 852.96;
$d = 14043.04

$d / (( $a / $b ) / 52)

Open in new window

Here's my actual code:
$c = ($a/$b);
$c1 = number_format($c , 2);
$c2 = ($c1/52);
$c3 = number_format($c2 , 3);
$c4 = ($d / $c3);

Open in new window

Result = 0.55908310370992
Should be: 561

I've tried different formatting and decimal placing, however, the end result is always off.  I know PHP isn't exact, however, I know it's possible to achieve the result I'm looking for as you can see from the data loaded directly from the database in the screenshot
Example ScreenshotAny help would be wonderful =)

Who is Participating?
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
Remove the number_format() statements.  That converts the number to text.  Then you try to divide by text which doesn't work as you would expect.  This results in 560.80953497126 .
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<title>PHP Math</title>
<h1>PHP Math</h1>
$a = 1110651.36;
$b = 852.96;
$d = 14043.04;

$c = ($a/$b);
echo $c."<br>";
//$c1 = number_format($c , 2);
$c1 = $c;
echo $c1."<br>";
$c2 = ($c1/52);
echo $c2."<br>";
//$c3 = number_format($c2 , 3);
$c3 = $c2;
echo $c3."<br>";
$c4 = ($d / $c3);

echo $c4;

Open in new window

btuskeyAuthor Commented:
Thank you Dave...thankful for your quick response.

I have to tell you, when I went in and removed the number_format, I still ended up with the same result as I posted above.  I couldn't believe, after looking at your comments again, I was able to see the woods through the trees and noticed I hadn't added a str_replace back in after all the different things I was trying as a resolve.  

As soon as I put the str_replace back in, it worked like a charm confirming I hadn't become dumber over the years, just a bit more blind with age haha.

Thank you again =)
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.