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
?>
10 个解决方案
#1
11
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.
我实际上认为你的解决方法和任何方法一样好。它简单明了,在这里谈论性能真的没有意义,所以就去吧。
#2
21
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
#3
8
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中移除0 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
?>
#4
7
I add the article Remove useless zero digits from decimals in PHP as a nicer way to clean the value without loose decimal if needed.
我添加文章删除PHP中小数的无用零位数作为一种更好的方法来清理没有宽松小数的值,如果需要的话。
#5
4
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)
#6
0
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;
#7
0
Warren.S answer helped me out. I didn't need the number_format function, so I just did this
沃伦。答案帮助了我。我不需要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;
#8
0
Since I could not find a flexible solution I wrote a simple function to get the best result:
由于我找不到灵活的解决方案,我写了一个简单的函数来获得最佳结果:
function getValueFormattedWithMinimalDecimals($value, $max_decimals = 2, $dec_point = ',', $thousands_sep = '') {
$bestNumberOfDecimals = -1;
$decimal = 0;
while ($decimal <= $max_decimals) {
$bestNumberOfDecimals = $decimal;
$valueDecimals = number_format($value, $decimal);
if (floatval($value) == $valueDecimals) {
break;
}
$decimal++;
}
if($bestNumberOfDecimals > 0 && number_format($value, $bestNumberOfDecimals) == number_format($value, 0)) {
$bestNumberOfDecimals = 0;
}
return number_format($value, $bestNumberOfDecimals, $dec_point, $thousands_sep);
}
#9
0
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);
-->$1,300,999
moneyform(2500.99);
-->$2,500.99
moneyform(2500.99, false);
-->2,500.99
#10
-1
What about
关于什么
number_format($value,2) - 0;
#1
11
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.
我实际上认为你的解决方法和任何方法一样好。它简单明了,在这里谈论性能真的没有意义,所以就去吧。
#2
21
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
#3
8
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中移除0 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
?>
#4
7
I add the article Remove useless zero digits from decimals in PHP as a nicer way to clean the value without loose decimal if needed.
我添加文章删除PHP中小数的无用零位数作为一种更好的方法来清理没有宽松小数的值,如果需要的话。
#5
4
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)
#6
0
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;
#7
0
Warren.S answer helped me out. I didn't need the number_format function, so I just did this
沃伦。答案帮助了我。我不需要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;
#8
0
Since I could not find a flexible solution I wrote a simple function to get the best result:
由于我找不到灵活的解决方案,我写了一个简单的函数来获得最佳结果:
function getValueFormattedWithMinimalDecimals($value, $max_decimals = 2, $dec_point = ',', $thousands_sep = '') {
$bestNumberOfDecimals = -1;
$decimal = 0;
while ($decimal <= $max_decimals) {
$bestNumberOfDecimals = $decimal;
$valueDecimals = number_format($value, $decimal);
if (floatval($value) == $valueDecimals) {
break;
}
$decimal++;
}
if($bestNumberOfDecimals > 0 && number_format($value, $bestNumberOfDecimals) == number_format($value, 0)) {
$bestNumberOfDecimals = 0;
}
return number_format($value, $bestNumberOfDecimals, $dec_point, $thousands_sep);
}
#9
0
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);
-->$1,300,999
moneyform(2500.99);
-->$2,500.99
moneyform(2500.99, false);
-->2,500.99
#10
-1
What about
关于什么
number_format($value,2) - 0;