[英]PHP show only significant (non-zero) decimals
In PHP (using built-in functions) I'd like to convert/format a number with decimal, so that only the non-zero decimals show. 在PHP中(使用内置函数)我想转换/格式化一个带小数的数字,这样只显示非零小数。 However, another requirement of mine is that if it's a number without a decimal value, I'd still like to show that zero. 然而,我的另一个要求是,如果它是一个没有小数值的数字,我仍然希望显示为零。 Examples: 例子:
9.000 -> 9.0
9.100 -> 9.1
9.120 -> 9.12
9.123 -> 9.123
rtrim($value, "0")
almost works. rtrim($value, "0")
几乎可以工作。 The problem with rtrim is that it leaves 9.000
as 9.
. rtrim的问题是它留下9.000
为9.
。 sprintf()
seemed like a candidate, but I couldn't get it to have a variable amount of decimals. sprintf()
似乎是一个候选者,但我不能让它有一个可变的小数位数。 number_format()
serves a different purpose, and those were all I could come up with... number_format()
用于不同的目的,而这些都是我能想到的......
Again, I'd like to point out that I am not looking for your homemade solutions to this, I'm looking for a way to accomplish this using internal PHP functionality. 再次,我想指出我不是在寻找你自己的解决方案,我正在寻找一种使用内部PHP功能来实现这一目标的方法。 I can write a function that will accomplish this easily myself, so hold answers like that. 我可以编写一个能够自己轻松完成此功能的函数,所以请保持这样的答案。
I don't think theres a way to do that. 我不认为这是一种方法。 A regex is probably your best solution: 正则表达式可能是您最好的解决方案:
$value = preg_replace('/(\.[0-9]+?)0*$/', '$1', $value);
Demo: 演示:
php> $a = array('0.000', '0.0001', '0.0101', '9.000', '9.100', '9.120', '9.123');
php> foreach($a as $b) { echo $b . ' => ' . preg_replace('/(\.[0-9]+?)0*$/', '$1', $b)."\n"; }
0.000 => 0.0
0.0001 => 0.0001
0.0101 => 0.0101
9.000 => 9.0
9.100 => 9.1
9.120 => 9.12
9.123 => 9.123
try something like this 尝试这样的事情
$number = 2.00;
echo floor_dec($number,$deg);
function floor_dec($number, $deg = null)
{
if ($deg == null)
return $number * 1000 / 1000;
else
return $number * pow(10, $deg) / pow(10, $deg);
}
will display "2" 将显示“2”
Shouldn't it be?: 不应该吗?:
$value = preg_replace('~0*$~', '', $value);
The PHP preg_replace syntax is PHP preg_replace语法是
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
If you want a built-in solution and you're using a PHP version later than 4.2 you could try floatval()
: 如果你想要一个内置的解决方案并且你使用的是4.2之后的PHP版本,你可以尝试使用floatval()
:
echo floatval(9.200);
prints 版画
9.2
but 但
echo floatval(9.123);
prints 版画
9.123
Hope this helps. 希望这可以帮助。
A trailing zero is significant: 尾随零是重要的:
Therefore, your requirement is quite unusual. 因此,您的要求非常不寻常。 That's the reason why no function exists to do what you want. 这就是为什么没有功能可以做你想要的事情的原因。
<?php
$numbers = array(
"9.000",
"9.100",
"9.120",
"9.123"
);
foreach($numbers as $number) {
echo sprintf(
"%s -> %s\n",
$number,
(float) $number == (int) $number ? number_format($number, 1) : (float) $number
);
}
?>
Output: 输出:
9.000 -> 9.0
9.100 -> 9.1
9.120 -> 9.12
9.123 -> 9.123
Out of the box that isn't possible because you have two different ways of treating the fragment of your floats. 开箱即用是不可能的,因为你有两种不同的方法来处理浮动片段。 You'll first have to determine how many non-zero numbers there are in your fragment and then act accordingly with sprintf. 您首先必须确定片段中有多少非零数字,然后使用sprintf进行相应操作。
<?php
$numbers = array(
'9.000',
'9.100',
'9.120',
'9.123',
);
foreach ($numbers as $number) {
$decimals = strlen(str_replace('0','', array_pop(explode('.', $number))));
$decimals = $decimals ?: 1;
echo $number . " => " . sprintf("%.{$decimals}f", $number);
echo "<br/>";
}
怎么样
preg_replace(/\\.$/,'.0',rtrim($value,'0'))
假设数字被编码为或转换为字符串,这是一个通用的方法:
$value = is_numeric($value) ? strval($value + 0) : $value;
My solution is to let php handle it as a number (is *1) and then treat it as a string (my example I was using percentages stored as a decimal with 2 decimal places): 我的解决方案是让php处理它作为一个数字(是* 1)然后将其视为一个字符串(我的例子我使用的百分比存储为小数点后2位):
printf('%s%% off', $value*1);
This outputs: 这输出:
0.00 => 0% off
0.01 => 0.01% off
20.00 => 20% off
20.50 => 20.5% off
rtrim($value, "0") almost works. rtrim($ value,“0”)几乎可以工作。 The problem with rtrim is that it leaves 9.000 as 9. rtrim的问题在于它将9.000留作9。
So just rtrim($value, "0.") and you're done. 所以只是rtrim($ value,“0”)而且你已经完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.