PHP 数字:小数点仅在需要时可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4113200/
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
PHP number: decimal point visible only if needed
提问by vitto
I'd like to know if exists some function to automatically format a number by it's decimal, so if I have:
我想知道是否存在一些函数来自动按十进制格式化数字,所以如果我有:
<?php
// $sql_result["col_number"] == 1,455.75
number_format ($sql_result["col_number"], 2, ".", "");
// will return 1455.75
// $sql_result["col_number"] == 1,455.00
number_format ($sql_result["col_number"], 2, ".", "");
// could I get 1455 instead of 1455.00?
?>
so my answer is if does exist some way to remove the decimals if I have DECIMAL data forma in my DB only when it's round?
所以我的答案是,如果我的数据库中有 DECIMAL 数据格式,只有在它是圆形的时候,是否确实存在某种方法来删除小数?
Or shoud I do something like that?
或者我应该这样做吗?
<?php
// $sql_result["col_number"] == 1,455.00
str_replace(".00", "", (string)number_format ($sql_result["col_number"], 2, ".", ""));
// will return 1455
?>
采纳答案by Emil H
I actually think that your workaround is as good as any. It's simple and clear, and there's really no point talking about performance here, so just go for it.
我实际上认为您的解决方法与任何方法一样好。它简单明了,在这里谈论性能真的没有意义,所以就去做吧。
回答by xDiff
floatval or simply casting to float
floatval 或简单地强制转换为浮动
php > echo floatval(7.00);
7
php > echo floatval(2.30);
2.3
php > echo floatval(1.25);
1.25
php > echo floatval(1.125);
1.125
php > echo (float) 7.00;
7
php > echo (float) 2.30;
2.3
php > echo (float) 1.25;
1.25
php > echo (float) 1.125;
1.125
回答by Halil ?zgür
As Emil says yours are good. But if you want to remove 0
from e.g. 7.50
too, I've got a suggestion, rtrim()
:
正如埃米尔所说,你的很好。但是如果你也想0
从 eg 中删除7.50
,我有一个建议,rtrim()
:
<?php
// if $sql_result["col_number"] == 1,455.50
rtrim(rtrim(number_format($sql_result["col_number"], 2, ".", ""), '0'), '.');
// will return 1455.5
?>
回答by Dan Leveille
You could also use rtrim(), which would remove excess 0s, in the case where you might want to keep one decimal place but not the excess zeros. (For example, 4.50 becomes 4.5.) Also allows you to change the number of decimal places from 2 to any other number.
您还可以使用 rtrim(),它会删除多余的 0,以防您可能希望保留一位小数但不保留多余的零。(例如,4.50 变为 4.5。)还允许您将小数位数从 2 更改为任何其他数字。
rtrim(rtrim((string)number_format($value, 2, ".", ""),"0"),".");
// 4.00 -> 4
// 4.50 -> 4.5
// 4.54000000 -> 4.54 (if you're doing more decimal places)
回答by vitto
I add the article Remove useless zero digits from decimals in PHPas a nicer way to clean the value without loose decimal if needed.
我添加了文章从 PHP中的小数中删除无用的零数字,这是一种更好的方法,可以在需要时清除不带松散小数的值。
回答by Tomas
Actually I think the cleanest way I can think of to do this for someone that just did a search looking for this sort of thing is to do this:
实际上,我认为我能想到的最干净的方法是为刚刚搜索此类事情的人执行此操作:
( number_format ($sql_result["col_number"], 2) * 100 ) / 100;
回答by Kevin J
If you are targeting US currency I like to use this method:
如果您的目标是美元,我喜欢使用这种方法:
function moneyform($number, $symbol = true) {
return str_replace(".00", "", money_format(($symbol? '%.2n' : "%!n"), $number));
}
moneyform(1300999);
-->,300,999
moneyform(2500.99);
-->,500.99
moneyform(2500.99, false);
-->2,500.99
回答by Danial
I've been accused of doing something like this:
我被指控做这样的事情:
floatval($foo) == intval($foo) ? number_format($foo) : number_format($foo,2);
回答by Felix Labayen
Mine since most quantity or pieces do not require decimal, this function will only show decimal when needed.
我的由于大多数数量或件数不需要小数,此功能仅在需要时显示小数。
str_replace(".00", "", number_format($this->pieces, 2));
回答by Sakushee
Warren.S answer helped me out. I didn't need the number_format function, so I just did this
Warren.S 的回答帮助了我。我不需要 number_format 函数,所以我只是这样做了
$value=$value-0;
But in the OP's case, he needs number_format to remove the commas. So this would work for him
但在 OP 的情况下,他需要 number_format 来删除逗号。所以这对他有用
$value=number_format ($sql_result["col_number"], 2, ".", "")-0;