从 PHP 中的小数中删除无用的零位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14531679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove useless zero digits from decimals in PHP
提问by vitto
I'm trying to find a fast way to remove zero decimalsfrom number values like this:
我试图找到一种快速的方法来zero decimals从这样的数值中删除:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
Does exists some optimized way to do that?
是否存在一些优化的方法来做到这一点?
回答by lafor
$num + 0does the trick.
$num + 0诀窍。
echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7
Internally, this is equivalent to casting to float with (float)$numor floatval($num)but I find it simpler.
在内部,这等效于强制转换为浮动(float)$num或浮动,floatval($num)但我发现它更简单。
回答by DiverseAndRemote.com
you could just use the floatvalfunction
你可以使用这个floatval功能
echo floatval('125.00');
// 125
echo floatval('966.70');
// 966.7
echo floatval('844.011');
// 844.011
回答by mpen
This is what I use:
这是我使用的:
function TrimTrailingZeroes($nbr) {
return strpos($nbr,'.')!==false ? rtrim(rtrim($nbr,'0'),'.') : $nbr;
}
N.B. This assumes .is the decimal separator. It has the advantage that it will work on arbitrarily large (or small) numbers since there is no float cast. It also won't turn numbers into scientific notation (e.g. 1.0E-17).
注意这假设.是小数点分隔符。它的优点是可以处理任意大(或小)的数字,因为没有浮点型转换。它也不会将数字转换为科学记数法(例如 1.0E-17)。
回答by Alexander Yancharuk
Simply adding +to your string variable will cause typecast to (float)and removes zeros:
简单地添加+到您的字符串变量将导致类型转换为(float)并删除零:
var_dump(+'125.00'); // double(125)
var_dump(+'966.70'); // double(966.7)
var_dump(+'844.011'); // double(844.011)
var_dump(+'844.011asdf');// double(844.011)
回答by Kai Noack
For everyone coming to this site having the same problem with commata instead, change:
对于访问此站点的每个人都遇到与逗号相同的问题,请更改:
$num = number_format($value, 1, ',', '');
to:
到:
$num = str_replace(',0', '', number_format($value, 1, ',', '')); // e.g. 100,0 becomes 100
If there are two zerosto be removed, then change to:
如果要删除两个零,则更改为:
$num = str_replace(',00', '', number_format($value, 2, ',', '')); // e.g. 100,00 becomes 100
More here: PHP number: decimal point visible only if needed
更多信息:PHP 数字:小数点仅在需要时可见
回答by Hemerson Varela
回答by Sajan Parikh
You should cast your numbers as floats, which will do this for you.
您应该将您的数字转换为浮点数,这将为您做到这一点。
$string = "42.422005000000000000000000000000";
echo (float)$string;
Output of this will be what you are looking for.
这将是您正在寻找的输出。
42.422005
42.422005
回答by Dakkaron
$x = '100.10';
$x = preg_replace("/\.?0*$/",'',$x);
echo $x;
There is nothing that can't be fixed with a simple regex ;)
没有什么是不能用简单的正则表达式解决的;)
回答by Joseph A.
Typecast to a float.
类型转换为float.
$int = 4.324000;
$int = (float) $int;
回答by vee
Due to this question is old. First, I'm sorry about this.
由于这个问题是旧的。首先,我对此感到抱歉。
The question is about number xxx.xx but in case that it is x,xxx.xxxxx or difference decimal separator such as xxxx,xxxx this can be harder to find and remove zero digits from decimal value.
问题是关于数字 xxx.xx 但如果它是 x,xxx.xxxxx 或不同的小数分隔符,如 xxxx,xxxx,这可能更难找到并从十进制值中删除零位。
/**
* Remove zero digits from decimal value.
*
* @param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
* @param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
* @return string Return removed zero digits from decimal value.
*/
function removeZeroDigitsFromDecimal($number, $decimal_sep = '.')
{
$explode_num = explode($decimal_sep, $number);
if (is_array($explode_num) && isset($explode_num[count($explode_num)-1]) && intval($explode_num[count($explode_num)-1]) === 0) {
unset($explode_num[count($explode_num)-1]);
$number = implode($decimal_sep, $explode_num);
}
unset($explode_num);
return (string) $number;
}
And here is the code for test.
这是测试的代码。
$numbers = [
1234,// 1234
-1234,// -1234
'12,345.67890',// 12,345.67890
'-12,345,678.901234',// -12,345,678.901234
'12345.000000',// 12345
'-12345.000000',// -12345
'12,345.000000',// 12,345
'-12,345.000000000',// -12,345
];
foreach ($numbers as $number) {
var_dump(removeZeroDigitsFromDecimal($number));
}
echo '<hr>'."\n\n\n";
$numbers = [
1234,// 12324
-1234,// -1234
'12.345,67890',// 12.345,67890
'-12.345.678,901234',// -12.345.678,901234
'12345,000000',// 12345
'-12345,000000',// -12345
'12.345,000000',// 12.345
'-12.345,000000000',// -12.345
'-12.345,000000,000',// -12.345,000000 STRANGE!! but also work.
];
foreach ($numbers as $number) {
var_dump(removeZeroDigitsFromDecimal($number, ','));
}

