假设我们有 0.00045。我想找到一种方法来计算小数点后“重要”零的数量(在这种情况下为 3)。我一直在尝试实施strpos
or substr
,但我遇到了困难。
其他前...
3.006405:应该返回“2”
0.0000062:应该返回“5”
9.0100000008:应该返回“1”
有任何想法吗?
strspn($num, "0", strpos($num, ".")+1)
strspn
求零序列的长度。strpos
找到小数点的位置,我们从过去的 1 位开始。
但是,这不起作用,0.0000062
因为它在转换为字符串时会转换为科学记数法:6.200000e-6
。当数字以 结尾时e-<exponent>
,零的数量是<exponent>-1
。您可以使用正则表达式检查这一点。
我决定研究一些不同的可能性来处理这个任务。
我必须说,Barmar 的解决方案易于阅读,并且非常适合不受科学记数法影响的浮点数。
作为一个思想实验,我决定制作几个正则表达式来完成同样的工作。(*即使您将整数值传递给正则表达式方法,它们仍然可以使用。)
preg_match_all()
,我使用“继续”元字符 ( \G
) 来匹配第一个.0
然后是零个或多个0
紧随其后的字符。preg_match()
“忘记它” ,然后从该位置匹配 1 个或多个零。\K
不幸的是,这需要额外调用str_len()
. 我通常只支持preg_
调用比非正则表达式方法更少的函数调用。受https://codereview.stackexchange.com/q/219601/141885的启发,我想编写一个函数来仅使用算术(猜测和检查)来计算小数点后的零。 虽然它不像其他的那样受到科学记数法的影响,但它并不完美,并且在不同的操作系统/版本/环境/yatta-yatta 上对浮点数/整数有限制(我不假装知道所有的 ins-and -out,但我知道存在差异)。 继续用您的项目数据尝试其中的一些方法,并随时对哪些方法有效,哪些方法无效发表评论。...如果你知道我为什么邀请你来教育我:)
最后,StackOverflow 上有很多帖子(这里是一篇)推荐使用标准 php 库BCMath。
代码:(演示)
function mathematical_zeros_after_dot($float) {
$float = abs($float); // remove any signs
$float -= (int)$float; // remove whole numbers from float
if ($float == 0) {
return "Rendered as 0";
}
$max = 20;
for ($x = 0; $x < $max; ++$x) { // for loop with a hard limit to avoid infinite loop
$float *= 10;
if ($float >= 1) {
return $x;
}
}
return "$max {exceeded}";
}
$floats = [
25.000000000022, // 10
0.0000062, // 5
0.020320, // 1
.505000, // 0
0, // 0
.000507, // 3
-.002009, // 2
1000, // 0
0.00, // 0
1 // 0
-1.0000000000004000004, // 12
981.0000000000000000000004000004 // 21
];
foreach ($floats as $float) {
echo "(Math) {$float} has " , mathematical_zeros_after_dot($float) , " zero(s)\n";
echo "Barmar {$float} has " , strspn($float, "0", strpos($float, ".")+1) , " zero(s)\n";
echo "(PMA) {$float} has " , preg_match_all('~(?:\.|\G(?!^))0~', $float) , " zero(s)\n";
echo "(PA) {$float} has " , (preg_match('~\.\K0+~', $float, $match) ? strlen($match[0]) : 0) , " zero(s)\n";
}
输出:
(Math) 25.000000000022 has 10 zero(s)
Barmar 25.000000000022 has 10 zero(s)
(PMA) 25.000000000022 has 10 zero(s)
(PA) 25.000000000022 has 10 zero(s)
(Math) 6.2E-6 has 5 zero(s)
Barmar 6.2E-6 has 0 zero(s)
(PMA) 6.2E-6 has 0 zero(s)
(PA) 6.2E-6 has 0 zero(s)
(Math) 0.02032 has 1 zero(s)
Barmar 0.02032 has 1 zero(s)
(PMA) 0.02032 has 1 zero(s)
(PA) 0.02032 has 1 zero(s)
(Math) 0.505 has 0 zero(s)
Barmar 0.505 has 0 zero(s)
(PMA) 0.505 has 0 zero(s)
(PA) 0.505 has 0 zero(s)
(Math) 0 has Rendered as 0 zero(s)
Barmar 0 has 0 zero(s)
(PMA) 0 has 0 zero(s)
(PA) 0 has 0 zero(s)
(Math) 0.000507 has 3 zero(s)
Barmar 0.000507 has 3 zero(s)
(PMA) 0.000507 has 3 zero(s)
(PA) 0.000507 has 3 zero(s)
(Math) -0.002009 has 2 zero(s)
Barmar -0.002009 has 2 zero(s)
(PMA) -0.002009 has 2 zero(s)
(PA) -0.002009 has 2 zero(s)
(Math) 1000 has Rendered as 0 zero(s)
Barmar 1000 has 3 zero(s)
(PMA) 1000 has 0 zero(s)
(PA) 1000 has 0 zero(s)
(Math) 0 has Rendered as 0 zero(s)
Barmar 0 has 0 zero(s)
(PMA) 0 has 0 zero(s)
(PA) 0 has 0 zero(s)
(Math) -3.9990233346998E-13 has 12 zero(s)
Barmar -3.9990233346998E-13 has 0 zero(s)
(PMA) -3.9990233346998E-13 has 0 zero(s)
(PA) -3.9990233346998E-13 has 0 zero(s)
(Math) 981 has Rendered as 0 zero(s)
Barmar 981 has 0 zero(s)
(PMA) 981 has 0 zero(s)
(PA) 981 has 0 zero(s)
匹配尾数中的第一个连续零:
$number = 123.0000000240003;
preg_match("/^(0+)/", explode('.', $number)[1], $matches);
echo strlen($matches[0]); // echoes 7