PHP编号:小数点仅在需要时可见

PHP编号:小数点仅在需要时可见,php,string,automation,integer,Php,String,Automation,Integer,我想知道是否存在自动按十进制数格式化数字的函数,因此如果我有: <?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["

我想知道是否存在自动按十进制数格式化数字的函数,因此如果我有:

<?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?
?>

所以我的答案是,如果我的数据库中有十进制数据形式,只有当它是四舍五入时,是否存在某种方法来去除小数

还是我应该做那样的事

<?php
    // $sql_result["col_number"] == 1,455.00
    str_replace(".00", "", (string)number_format ($sql_result["col_number"], 2, ".", ""));
    // will return 1455
?>


事实上,我认为您的解决方案与其他解决方案一样好。它简单明了,在这里谈论性能是毫无意义的,所以就去做吧。

正如埃米尔所说,你的性能很好。但是,如果您也想从例如
7.50
中删除
0
,我有一个建议,
rtrim()


事实上,我认为我能想到的最干净的方法是为刚刚搜索过这类东西的人这样做:

( number_format ($sql_result["col_number"], 2) * 100 ) / 100;
如果需要的话,我添加这篇文章是为了更好地清除数值,而不会出现松散的小数点。

怎么样

number_format($value,2) - 0;

沃伦的回答帮助了我。我不需要数字格式函数,所以我只做了这个

$value=$value-0;
但在OP的例子中,他需要数字_格式来删除逗号。所以这对他来说是可行的

$value=number_format ($sql_result["col_number"], 2, ".", "")-0;

floatval或简单地铸造成float

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
您还可以使用rtrim(),它将删除多余的0,如果您希望保留小数点后一位,但不保留多余的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)

由于找不到灵活的解决方案,我编写了一个简单的函数以获得最佳结果:

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);
}
函数GetValueFormattedwithMinimumDecimals($value、$max_decimals=2、$dec_point='、'、$数千_sep=''){
$bestNumberOfDecimals=-1;
$decimal=0;
而($decimal 0&&number\u格式($value,$bestNumberOfDecimals)==number\u格式($value,0)){
$bestNumberOfDecimals=0;
}
返回数字格式($value、$bestNumberOfDecimals、$dec_point、$数千_sep);
}

如果您以美元为目标货币,我喜欢使用以下方法:

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

我被指控做了这样的事:

 floatval($foo) == intval($foo) ? number_format($foo) : number_format($foo,2);

由于大多数数量或件数不需要小数,此函数仅在需要时显示小数

str_replace(".00", "", number_format($this->pieces, 2));

我认为在这种情况下使用
rtrim
会有一个bug,例如,
rtrim('1230.00','0')将给出123而不是1230.V很好的解决方案Halil..谢谢:)@bobo该漏洞与Halil给出的代码不存在,该代码使用2个按顺序执行的RTRIM。这里有一个简洁的答案:@JohnK,我刚刚意识到你给出的链接也是他去年提出的问题。Hahahacasting to float是目前为止最好的选择。任何想将其应用于字符串的人都应该注意带有逗号的值,如“1000.99”,浮点值将返回1。@YohanesAI我知道。我在警告大家。您只需添加0
1455.00+0
替换解决方案将使用欧洲国家格式的数字表示,如德国或西班牙。例如,德国的9000是9.000,00,这将变成90,00,如何更改小数和千分隔符@FarzherI很惊讶这没有更多的投票,因为这是这个问题的最干净/最优雅的解决方案。如果你有其他字符用于小数分隔,这是最好的方法。这似乎已经将数字从10400.00减少到104,对我来说,完美的答案加上解释+1:)让我度过了美好的一天唯一的问题是
money\u format
是目前已弃用,将来将被删除。。裁判。
str_replace(".00", "", number_format($this->pieces, 2));