我将数字2.50
存储在一个名为$price
的变量中,我可以传递给一个函数,它会识别值2.50
但是当我尝试将它与{{1}相乘时我得到答案2
。我假设PHP正在截断原始值,然后我认为它可能会将其解释为String,因为我从XML文档中获取了值,但manual表示它会正确地将其识别为{{1}如果它是一个字符串。如果有人能对此有所了解,我会非常感激。
编辑:以下是我正在使用的代码 -
4
返回3.4的事实是真正的问题,即使它应该是2.50
,就好像它正在进行$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;
}
;我在原始示例中选择2 * 1.7
,因为看到问题更为明显。
更新
在2.5
之后,如果我用2.5 * 2
的演员表示回复1.8 * $price
我得到了这个:$price
这两个点有点吓到我......
答案 0 :(得分:2)
在函数开头使用floatval将字符串解析为浮点数
function getRealPrice($price, $type) {
$price = floatval($price);
答案 1 :(得分:0)
这是一种疯狂的猜测,但你应该确保值为'2.50'而不是'2,50'。