数值意外向下舍入

发布于 2024-12-24 03:31:00 字数 298 浏览 3 评论 0原文

我有一个循环计算几个收入值,然后将它们加在一起,如下所示:

$SalesGrowth = $C2012Sales+$C2011Sales;

在某些情况下,这有效,并且我得到了预期的结果,例如: 761.9 + 759.0 = 1520.9

在其他情况下,PHP 似乎随机决定错误地舍入(??) 并更改单位 (??),我得到:

8,788.0 + 8,794.3 = 16

这是怎么回事?我什至尝试回显由空格分隔的单独销售值,并且它们显示正确,因此基本数字没有错误。

I have a loop that calculates a couple revenue values then adds them together, like this:

$SalesGrowth = $C2012Sales+$C2011Sales;

In some cases, this works, and I get the expected, e.g.: 761.9 + 759.0 = 1520.9

In others, it looks like PHP randomly decides to round incorrectly (??) AND change the units (??) and I get:

8,788.0 + 8,794.3 = 16

What is going on here? I've even tried echoing out the separate sales values separated by a space, and they show up correctly, so the underlying figures aren't wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

℡寂寞咖啡 2024-12-31 03:31:00

解释为数字时,8,788.0 就是 8,解析在逗号处停止。

如果您想允许像千位分隔符这样的花招,您将需要一些区域设置感知的数字解析。


更新:如果您有Zend Framework,您可以这样做:

require_once('Zend/Locale/Format.php');

$locale = new Zend_Locale('en_GB'); // #1

$v = "8,410.5";
$n = Zend_Locale_Format::getNumber($v, array('locale' => $locale,'precision' => 3));

echo 2 * $number;   // prints "16821"

您可以尝试从环境中获取它,而不是对区域设置进行硬编码:new Zend_Locale(setlocale(LC_ALL, ""))

Interpreted as a number, 8,788.0 is just 8, and parsing stops at the comma.

You'll need some locale-aware number parsing if you want to allow gimmicks like thousands-separators.


Update: If you have the Zend Framework, you can do this:

require_once('Zend/Locale/Format.php');

$locale = new Zend_Locale('en_GB'); // #1

$v = "8,410.5";
$n = Zend_Locale_Format::getNumber($v, array('locale' => $locale,'precision' => 3));

echo 2 * $number;   // prints "16821"

Instead of hard-coding the locale, you could try and take it from the environment: new Zend_Locale(setlocale(LC_ALL, ""))

时光礼记 2024-12-31 03:31:00

伙计,逗号问题......

在添加数字之前删除数字中的所有逗号......

str_replace(",","",$no1);

Dude the comma issue....

remove all the commas from the numbers before adding them...

str_replace(",","",$no1);
会发光的星星闪亮亮i 2024-12-31 03:31:00

这非常简单...当您要求 PHP 使用 + 运算符时,它会隐式地将这些字符串(例如 "8,788.0")转换为数值。由于您有一个 , 字符,因此它终止了数字的有用性,并导致它被解释为 8。依此类推...

去掉非 [0-9.] 字符,效果会更好。

This is pretty simple... When you ask PHP to use the + operator, it will implicitly convert these strings such as "8,788.0" to an numeric value. Since you have a , character, it terminates the usefulness of the number, and it results in it being interpreted as 8. And so on...

Get rid of the non [0-9.] characters and it will work better.

错々过的事 2024-12-31 03:31:00

请注意,761.9 是有效数字,而 8,788.0 则不是(从 PHP 的角度来看)。

因此,数字上下文中的 8,788.0 将计算为 8,就像 8,794.3 一样。 8+8 = 16。

要解决此问题,请处理数据以使数字格式正确。

Notice that 761.9 is a valid number, while 8,788.0 is not (from PHP's point of view).

So 8,788.0 in number context will evaluate as 8, just like 8,794.3. And 8+8 = 16.

To fix this problem, process your data to make numbers formatted properly.

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