Page 1 of 1

Wrong result in multiplication

Posted: Thu Jun 09, 2011 12:13 am
by bob_marely
Hello,

I have the following variables in my code:
$x =4;
$y= 123.4791187

When I multiply them I expect to get 493.9164748 but instead I get 492.
I've tried to do some conversions but it still didn't work..

Can any of you please advise what am I missing here?

Many thanks in advance,
Bob

Re: Wrong result in multiplication

Posted: Thu Jun 09, 2011 3:31 am
by Weirdan

Code: Select all

❯ php -r 'var_dump(4*123.4791187);'                                                                                                                                                 
float(493.9164748)
probably you need to remove conversions because without them, as you see, everything is working like you expect.

Re: Wrong result in multiplication

Posted: Thu Jun 09, 2011 6:12 am
by bob_marely
thanks for your answer!

It doesn't work with or without the conversions. I only tried the conversions as a solution and I removed it when I realized it doesn't work.

Any other sugesstions?

Re: Wrong result in multiplication

Posted: Thu Jun 09, 2011 11:10 am
by Jade
Try adding a decimal to the first number.

Code: Select all

$x = 4.0;
$y = 123.4791187;

echo ($x * $y);

Re: Wrong result in multiplication

Posted: Thu Jun 09, 2011 11:33 am
by social_experiment

Code: Select all

$x = 4;
$y = 123.4791187;

$z = $x * $y;

echo $z;
// 493.9164748
bob_marely wrote:Can any of you please advise what am I missing here?
Paste the code that you use for the 'multiplication'