-1

I have the following code:

$convertedCredits = round($userBalance['points'] / $convertionPoints) * $convertionPrice;
echo 'points: ' . $userBalance['points'] . ' - to: ' . $convertedCredits;

Example data (I know there is no point in dividing by 9 and multiply by 9 but that data is dynamic):

(6000/9) * 9

My echo results in the following:

points: 6000 - to: 60030

Why does that result in 60030?

Christophe
  • 4,798
  • 5
  • 41
  • 83

1 Answers1

5

Your php code says:

round(6000 / 9) * 9

So php computes 600 / 9, yielding 666.66667.

Then it uses round() on that figure, and gets 667.

Then it multiples the result by 9, yielding 6003.

It's behaving exactly as designed.

Try changing your echo line to this and see what happens. I bet you get

echo '(points: ' . $userBalance['points'] . ' - to: ' . $convertedCredits . ')  ';

I bet the extra zero comes from somewhere else in your program and you get

(points: 6000 - to: 6003)  0

Either that or your convertionPrice value is 90.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • I first tried without the round and had the same result. and no it does not behave as expected. Your result is correct however my result gives me 60030 (sixty thousand instead of six thousand) – Christophe Jun 15 '14 at 19:06
  • oooh yes. It's a response from an ajax call. ... god I feel so stupid. In my defense I was watching the World cup while working :p no more WK during programming :p – Christophe Jun 15 '14 at 20:14