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

发布于 2024-09-30 13:57:06 字数 646 浏览 2 评论 0原文

我想知道是否存在一些函数可以按小数自动格式化数字,所以如果我有:

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

那么我的答案是,如果我的数据库中有 DECIMAL 数据格式(仅当它是圆形时),是否存在某种方法来删除小数?

或者我应该做类似的事情吗?

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

不甘平庸 2024-10-07 13:57:06

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

floatval or simply casting to 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
萧瑟寒风 2024-10-07 13:57:06

我实际上认为你的解决方法和任何方法一样好。简单明了,这里讨论性能确实没有意义,直接说吧。

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.

如梦 2024-10-07 13:57:06

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

<?php
    // if $sql_result["col_number"] == 1,455.50
    rtrim(rtrim(number_format($sql_result["col_number"], 2, ".", ""), '0'), '.');
    // will return 1455.5
?>

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():

<?php
    // if $sql_result["col_number"] == 1,455.50
    rtrim(rtrim(number_format($sql_result["col_number"], 2, ".", ""), '0'), '.');
    // will return 1455.5
?>
抱着落日 2024-10-07 13:57:06

如果您可能想保留一位小数但不想保留多余的零,您还可以使用 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)

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(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)
找回味觉 2024-10-07 13:57:06

实际上,我认为对于刚刚搜索此类内容的人来说,我能想到的最干净的方法就是这样做:

( number_format ($sql_result["col_number"], 2) * 100 ) / 100;

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;
世态炎凉 2024-10-07 13:57:06

我被指责做了这样的事情:

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

I've been accused of doing something like this:

 floatval($foo) == intval($foo) ? number_format($foo) : number_format($foo,2);
捂风挽笑 2024-10-07 13:57:06

如果您的目标是美元,我喜欢使用这种方法:

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

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
一袭水袖舞倾城 2024-10-07 13:57:06

我的,因为大多数数量或件数不需要小数,所以此功能只会在需要时显示小数。

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

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));
欲拥i 2024-10-07 13:57:06

沃伦的回答帮助了我。我不需要 number_format 函数,所以我只是这样做了

$value=$value-0;

但在OP的情况下,他需要 number_format 来删除逗号。所以这对他有用

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

Warren.S answer helped me out. I didn't need the number_format function, so I just did this

$value=$value-0;

But in the OP's case, he needs number_format to remove the commas. So this would work for him

$value=number_format ($sql_result["col_number"], 2, ".", "")-0;
萌︼了一个春 2024-10-07 13:57:06

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

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);
}

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);
}
萌化 2024-10-07 13:57:06

怎么样

number_format($value,2) - 0;

What about

number_format($value,2) - 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文