PHP长十进制数问题
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用:
如果您不住在英国,请使用 number_format($real_ctr, 2, ".", "") 。
this is working for me:
and use number_format($real_ctr, 2, ".", ""), if you don't live in UK.
它接缝
$ctr
从 XML 解析器得到的是一个字符串。使用:确保您的变量是浮点型。
It seams
$ctr
what you get from the XML parser is a string. Use:to make sure your variable is a float.
浮点和双精度类型变量的本质是不能依赖它们来保证最后一位的精度。查看手册: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.phpThe 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它被作为字符串读取。您是否将其设置为等于 0.00529440333938,或者将其设置为您认为等于 0.00529440333938 的值?在赋值语句之后尝试一下,看看它是否有效:
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: