PHP; eBay API 返回的数字总和无效

发布于 2024-12-28 10:53:35 字数 417 浏览 2 评论 0原文

默认情况下,我似乎对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×眷恋的温暖 2025-01-04 10:53:35

确保两个值实际上都是整数或浮点数,而不是字符串。将字符串相互求和可能会导致奇怪的行为。

试试这个:

$ship = (float) $item->shippingInfo->shippingServiceCost;
$price = (float) $item->sellingStatus->currentPrice;
$sell = ($ship + $price);

现在 $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:

$ship = (float) $item->shippingInfo->shippingServiceCost;
$price = (float) $item->sellingStatus->currentPrice;
$sell = ($ship + $price);

Now $sell should consist of a float. You can use (float) to cast the variable to a floating variable.

孤芳又自赏 2025-01-04 10:53:35

一般来说,看一下类型转换,这在为 PHP 开发人员提供多种场景。

Take a look at typecasting in general, as is quite important in a variety of scenarios for you as a PHP developer.

红颜悴 2025-01-04 10:53:35

问题不在于 $ship 是一个带有 5.95 的字符串 .. 它将被正确转换为浮点数,但我认为它是 .. 5,95 并且会被转换到 5

<?php
$ship = "5,95";
$price = "18,55";
$sell = ($ship + $price);
echo "$sell = ($ship + $price)";

?>

输出:

23 = (5,95 + 18,55)

转换:

<?php
$ship = (float) "5,95";
$price = (float) "18,55";
$sell = ($ship + $price);
echo "$sell = ($ship + $price)";
?>

输出:

23 = (5 + 18)

使用:

$ship = str_replace(',','.',$ship);
$price = str_replace(',','.',$price); 

它应该可以工作!

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 5

<?php
$ship = "5,95";
$price = "18,55";
$sell = ($ship + $price);
echo "$sell = ($ship + $price)";

?>

output:

23 = (5,95 + 18,55)

convert:

<?php
$ship = (float) "5,95";
$price = (float) "18,55";
$sell = ($ship + $price);
echo "$sell = ($ship + $price)";
?>

output:

23 = (5 + 18)

Use:

$ship = str_replace(',','.',$ship);
$price = str_replace(',','.',$price); 

And it should work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文