Wrong result in multiplication

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bob_marely
Forum Newbie
Posts: 5
Joined: Mon Jan 25, 2010 2:46 am

Wrong result in multiplication

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Wrong result in multiplication

Post 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.
bob_marely
Forum Newbie
Posts: 5
Joined: Mon Jan 25, 2010 2:46 am

Re: Wrong result in multiplication

Post 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?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Wrong result in multiplication

Post by Jade »

Try adding a decimal to the first number.

Code: Select all

$x = 4.0;
$y = 123.4791187;

echo ($x * $y);
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Wrong result in multiplication

Post 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'
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply