Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 修剪尾随零而不强制转换为浮动_Php_Regex - Fatal编程技术网

Php 修剪尾随零而不强制转换为浮动

Php 修剪尾随零而不强制转换为浮动,php,regex,Php,Regex,让我们再试一次,但要更明确一点 我正在将数字打印到屏幕上,希望通过去掉小数点后的尾随零,让它们对用户更友好。我曾尝试将其转换为浮点型,但这种解决方案并不适合,因为我处理的数字是0.00000001,这些数字与0.1-e7类似,这对最终用户来说是不友好的 我需要一个解决方案,其中的数字如下 12.02000000 12.00000000 0.00000001 …可以打印到屏幕上,如 12.02 12 0.00000001 使用rtim可以杀死10000个这样的数字。数字\格式需要知道小数位数,

让我们再试一次,但要更明确一点

我正在将数字打印到屏幕上,希望通过去掉小数点后的尾随零,让它们对用户更友好。我曾尝试将其转换为浮点型,但这种解决方案并不适合,因为我处理的数字是0.00000001,这些数字与0.1-e7类似,这对最终用户来说是不友好的

我需要一个解决方案,其中的数字如下

12.02000000
12.00000000
0.00000001
…可以打印到屏幕上,如

12.02
12
0.00000001

使用rtim可以杀死10000个这样的数字。数字\格式需要知道小数位数,可以是0到8。将铸件浇铸为浮动不起作用。我确信有一些正则表达式可以解决这个问题。

这是您想要的正则表达式解决方案:

$repl = preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', $numstr);
测试:

echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '12.02000000') . "\n";
echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '12.00000000') . "\n";
echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '0.0000000100000000') . "\n";
echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '100000000') . "\n";
输出:

12.02
12
0.00000001
100000000

以下是您想要的正则表达式解决方案:

$repl = preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', $numstr);
测试:

echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '12.02000000') . "\n";
echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '12.00000000') . "\n";
echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '0.0000000100000000') . "\n";
echo preg_replace('/(?>\.0+$|(\.\d*[^0])0+$)/', '$1', '100000000') . "\n";
输出:

12.02
12
0.00000001
100000000

在搜索了一下internet之后,我发现了一些有用的东西:

function floattostr( $val )
{
    preg_match( "#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o );
    return $o[1].sprintf('%d',$o[2]).($o[3]!='.'?$o[3]:'');
}
我想这会给你所需要的输出


我找到了

在搜索了一下互联网后,我发现了一些有用的东西:

function floattostr( $val )
{
    preg_match( "#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o );
    return $o[1].sprintf('%d',$o[2]).($o[3]!='.'?$o[3]:'');
}
我想这会给你所需要的输出


我发现了一个稍微简单一点的解决方案:

preg_replace("/(\.0*[^0]+)0*|\.0*/", '\1', $number)

一个稍微简单的解决方案:

preg_replace("/(\.0*[^0]+)0*|\.0*/", '\1', $number)

以下是一种非正则表达式的方法:

if (strpos($number, '.') !== false) {
    $number = rtrim($number, '.0');
}
三值运算符样式:

$number = strpos($number, '.') !== false ? rtrim($number, '.0') : $number;

以下是一种非正则表达式的方法:

if (strpos($number, '.') !== false) {
    $number = rtrim($number, '.0');
}
三值运算符样式:

$number = strpos($number, '.') !== false ? rtrim($number, '.0') : $number;

非常好,非常感谢你花时间在这件事上。非常欢迎你,很高兴它为你解决了问题。非常好,非常感谢你花时间在这件事上。非常欢迎你,很高兴它为你解决了问题。