PHP; eBay API 返回的数字总和无效
默认情况下,我似乎对 PHP 四舍五入(某种)数字有疑问,我正在调用 eBay API 并获取商品的价格和邮资。我需要在将这两个数字输入数据库之前将它们相加,问题似乎是当我将这两个数字相加时,它们会返回一个奇怪的值:
shipping = 5.95
item price = 18.55
如果我将这两个数字相加,我会得到 23< /代码>。
$ship = $item->shippingInfo->shippingServiceCost;
$price = $item->sellingStatus->currentPrice;
$sell = ($ship + $price);
我需要告诉php返回的值是一个数字吗?
I seem to be having an issue with PHP rounding (sort of) numbers by default, I am calling the eBay API and getting back the price of an item and the postage. I need to add these two numbers before entering them into my database, the issue seems to be when I add the two numbers up they come back with a strange value:
shipping = 5.95
item price = 18.55
If I add the two up I get a result of 23
.
$ship = $item->shippingInfo->shippingServiceCost;
$price = $item->sellingStatus->currentPrice;
$sell = ($ship + $price);
Do I need to tell php the returned value is a number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保两个值实际上都是整数或浮点数,而不是字符串。将字符串相互求和可能会导致奇怪的行为。
试试这个:
现在 $sell 应该由一个浮点数组成。您可以使用
(float)
将变量转换为浮动变量。Make sure both of your values are actually integers or floats, instead of strings. Summing strings to eachother could result in strange behavior.
Try this:
Now $sell should consist of a float. You can use
(float)
to cast the variable to a floating variable.一般来说,看一下类型转换,这在为 PHP 开发人员提供多种场景。
Take a look at typecasting in general, as is quite important in a variety of scenarios for you as a PHP developer.
问题不在于 $ship 是一个带有
5.95
的字符串 .. 它将被正确转换为浮点数,但我认为它是 ..5,95
并且会被转换到 5输出:
转换:
输出:
23 = (5 + 18)
使用:
它应该可以工作!
The problem is not the $ship is a string with
5.95
.. that would be properly converted to float but I think it is ..5,95
and that would be converted to 5output:
convert:
output:
23 = (5 + 18)
Use:
And it should work!