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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer 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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.