I have the number 2.50 stored in a variable called $price which I can pass to a function and it recognizes the value 2.50 however when I try and multiply it with 2 I get the answer 4. I assumed PHP is truncating the original value and then I thought it might be interpreting it as a String as I am getting the value out of an XML document but the manual says that it will correctly recognize it as 2.50 if it were a String. If anyone could shed some light on this I’d really appreciate it.
Edit: Here’s the code I am using-
$Price = new Prices();
// Traverse XML document
foreach ($simpleXML->product as $product) {
// Gather info from the product array
$price = $product->price;
$type = $product->type;
$price = $Price->getRealPrice($price, $type);
}
function getRealPrice($price, $type) {
// echo $price returns 2.50
// Switch statement on type
case 'Keychain':
return 1.7 * $price; // This returns 3.4
break;
}
The fact that 3.4 is being returned is the real problem, it’s as if it is doing 2 * 1.7 even though it should be 2.5; I chose 2.5 * 2 in my original example because it was more obvious to see the problem.
Update:
After I do 1.8 * $price if I echo $price with cast of double I get this: 2.503.6 The two dots kinda freak me out…
Parse the string to a float by using floatval this in the beginning of your function