PHP长十进制数问题

发布于 2024-11-14 07:05:55 字数 409 浏览 2 评论 0原文

我使用 php 解析 xml,我的变量之一收到一个长十进制值。

$ctr = 0.00529440333938;

但我需要将这个数字设置为 0.52%,因此我尝试乘以 100 并强制保留 2 位小数。

$real_ctr = $ctr * 100;
echo number_format($real_ctr, 2) . "%";

我得到的结果是 0.00% 而不是 0f 0.52%

然后我尝试添加 1 只是为了测试目的

$real_ctr = $ctr + 1;

,我得到的结果是 0.005294403339381。最后加1。

有什么建议我可以如何解决这个问题吗?

Im parsing xml using php and one of my variables receive a long decimal value.

$ctr = 0.00529440333938;

But I need to make this number as 0.52%, so I tried multiplying by 100 and forcing just 2 decimals.

$real_ctr = $ctr * 100;
echo number_format($real_ctr, 2) . "%";

I get as result 0.00% instead 0f 0.52%

And then I tried adding 1 just for testing purpose

$real_ctr = $ctr + 1;

and I get as result 0.005294403339381. It adds 1 at the end.

Any suggestions how can I get around this issue?

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

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

发布评论

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

评论(4

明明#如月 2024-11-21 07:05:55

这对我有用:

$ctr = 0.00529440333938; 

$real_ctr = (double)$ctr * 100; 
$real_ctr = number_format($real_ctr, 2, ".", "") . "%";
$real_ctr = $ctr + 1;

如果您不住在英国,请使用 number_format($real_ctr, 2, ".", "") 。

this is working for me:

$ctr = 0.00529440333938; 

$real_ctr = (double)$ctr * 100; 
$real_ctr = number_format($real_ctr, 2, ".", "") . "%";
$real_ctr = $ctr + 1;

and use number_format($real_ctr, 2, ".", ""), if you don't live in UK.

负佳期 2024-11-21 07:05:55

它接缝 $ctr 从 XML 解析器得到的是一个字符串。使用:

$ctr = floatval($ctr); // or (double)$ctr; or (float)$ctr;

确保您的变量是浮点型。

It seams $ctr what you get from the XML parser is a string. Use:

$ctr = floatval($ctr); // or (double)$ctr; or (float)$ctr;

to make sure your variable is a float.

风和你 2024-11-21 07:05:55

浮点和双精度类型变量的本质是不能依赖它们来保证最后一位的精度。查看手册:http://php.net/manual/en/language。 types.float.php

在使用浮点数之前了解它们非常重要 - 如果您正在进行任何类型的财务计算,由于其固有的不精确性,浮点数会给您带来意想不到的结果。如果您需要精确的结果,那么您最好坚持使用双打,尤其是如果您正在处理与金钱相关的任何事情。

因此,请确保您使用的是您期望的号码类型。您遇到了这样的问题:由于 PHP 的松散类型,您的数字被转换为浮点数/双精度数。查看有关类型杂耍的文档:http://www. php.net/manual/en/language.types.type-juggling.php,那里可能有一些有用的信息。

基本上,您需要确保您的数字在计算过程中保持为整数,您需要防止 PHP 为您“决定”类型。查看 intval,或 BC 精度数学函数,如果您需要深入了解: http://www.php.net /manual/en/ref.bc.php

The nature of floating-point and double type variables is that they are not able to be relied upon for last-digit accuracy. Check out the manual: http://php.net/manual/en/language.types.float.php

It is important to understand floats before you use them - if you're doing any kind of financial calculations, floats will give you unexpected results because of their inherent imprecision. You're better off sticking to doubles if you need precise results, especially if you're working with anything related to money.

So, make sure you're using the type of number you're expecting. You're running into the issue where your number is being turned into a float/double because of PHP's loose typing. Check out the docs on type-juggling: http://www.php.net/manual/en/language.types.type-juggling.php, there may be some useful information there.

Basically, you'll need to ensure that your numbers are staying as integers through your calculations, you need to prevent PHP from "deciding' the type for you. Look in to intval, or the BC precision math functions, if you need to get that deep: http://www.php.net/manual/en/ref.bc.php

强辩 2024-11-21 07:05:55

它被作为字符串读取。您是否将其设置为等于 0.00529440333938,或者将其设置为您认为等于 0.00529440333938 的值?在赋值语句之后尝试一下,看看它是否有效:

$ctr = floatval($ctr);

It's being read as a string. Are you literally setting it equal to 0.00529440333938, or are you setting it equal to something that you think is equal to 0.00529440333938? Try this after the assignment statement and see if it works:

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