使用 printf 进行千位分隔符

发布于 2024-11-04 11:49:59 字数 1727 浏览 4 评论 0原文

printf 中的千位分隔符(如逗号)是如何实现的?

示例:

printf("<td class='number'>%d</td>", $totals['Sold']); //need thousand separated 
printf("<td class='number'>%.2f</td>", $totals['Fees']); //need thousand separated

更新这是我最初的内容:

foreach(array_keys($totals) as $key){
        if(is_numeric($totals[$key])){
            $totals[$key] = number_format($totals[$key],2);
        }
    }

    echo "</tbody>
        <tfoot><tr>";
    echo "<td class='text' colspan='4'>Totals:</td>";
    echo "<td class='number'>{$totals['Bought']}</td>";
    echo "<td class='number'>{$totals['Sold']}</td>";
    echo "<td class='number'>{$totals['Fees']}</td>";
    echo "<td class='number'>{$totals['Realized']}</td>";
    echo "<td class='number'>{$totals['Net']}</td>";
    echo "<td colspan='3'>{$totals['EOD Price']}</td>";
    echo "</tr>
        </tfoot>";

我希望它是这样的:

echo "</tbody>
        <tfoot><tr>";
    echo "<td class='text' colspan='3'>Totals:</td>";
    printf("<td class='number'>%d</td>", $totals['Bought']) ;
    printf("<td class='number'>%d</td>", $totals['Sold']) ;
    printf("<td class='number'>%.2f</td>", $totals['Fees']) ;
    printf("<td class='number'>%.2f</td>", $totals['Realized']) ;
    printf("<td class='number'>%.2f</td>", $totals['Net']) ;
    printf("<td colspan='3'>%.2f</td>", $totals['EOD Price']) ;
    echo "</tr>
        </tfoot>";

但我需要逗号

How do do a thousand separator (like a comma) in a printf?

example:

printf("<td class='number'>%d</td>", $totals['Sold']); //need thousand separated 
printf("<td class='number'>%.2f</td>", $totals['Fees']); //need thousand separated

UPDATE this is what i had originally:

foreach(array_keys($totals) as $key){
        if(is_numeric($totals[$key])){
            $totals[$key] = number_format($totals[$key],2);
        }
    }

    echo "</tbody>
        <tfoot><tr>";
    echo "<td class='text' colspan='4'>Totals:</td>";
    echo "<td class='number'>{$totals['Bought']}</td>";
    echo "<td class='number'>{$totals['Sold']}</td>";
    echo "<td class='number'>{$totals['Fees']}</td>";
    echo "<td class='number'>{$totals['Realized']}</td>";
    echo "<td class='number'>{$totals['Net']}</td>";
    echo "<td colspan='3'>{$totals['EOD Price']}</td>";
    echo "</tr>
        </tfoot>";

and I want it to be come something like:

echo "</tbody>
        <tfoot><tr>";
    echo "<td class='text' colspan='3'>Totals:</td>";
    printf("<td class='number'>%d</td>", $totals['Bought']) ;
    printf("<td class='number'>%d</td>", $totals['Sold']) ;
    printf("<td class='number'>%.2f</td>", $totals['Fees']) ;
    printf("<td class='number'>%.2f</td>", $totals['Realized']) ;
    printf("<td class='number'>%.2f</td>", $totals['Net']) ;
    printf("<td colspan='3'>%.2f</td>", $totals['EOD Price']) ;
    echo "</tr>
        </tfoot>";

But I need the commas

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

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

发布评论

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

评论(4

归途 2024-11-11 11:49:59

让你的代码变得漂亮并且不要全部重复。只需打破 PHP 上下文即可执行此操作,然后使用 number_format 代替 printf 进行 echo:

</tbody>
<tfoot>
    <tr>
        <td ...>Totals</td>
        <td ...><?php echo number_format($totals['Bought'], 0); ?></td>
        <td ...><?php echo number_format($totals['Sold'], 0); ?></td>
        <td ...><?php echo number_format($totals['Fees'], 2); ?></td>
        ...
    </tr>
</tfoot>

Make your code pretty and don't echo it all. Just break out of PHP context to do it, and then instead of printf just echo using number_format:

</tbody>
<tfoot>
    <tr>
        <td ...>Totals</td>
        <td ...><?php echo number_format($totals['Bought'], 0); ?></td>
        <td ...><?php echo number_format($totals['Sold'], 0); ?></td>
        <td ...><?php echo number_format($totals['Fees'], 2); ?></td>
        ...
    </tr>
</tfoot>
顾铮苏瑾 2024-11-11 11:49:59

您可以使用 number_format 函数。

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

http://php.net/manual/en/function.number-format.php

You can use the number_format function.

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

http://php.net/manual/en/function.number-format.php

呢古 2024-11-11 11:49:59

除了 ircmaxell 的答案之外:

如果您有一个多语言软件,并且您不想每次调用 number_format 时都传递 dec_point 和数千_sep 参数,您可以使用自定义函数来检查当前区域设置以确定当前的小数点和千位分隔符如下所示:

<?php
    /**
     * Calls {@link number_format} with automatically determining dec_point and thousands_app
     * according to the current locale in case they are null or omitted.
     * @param float $number The number being formatted.
     * @param int $decimals [optional] Sets the number of decimal points.
     * @param string|null $dec_point [optional] Decimal point character (determined automatically
     *        when set to null or omitted).
     * @param string|null $thousands_sep [optional] Thousands separator character (determined
     *        automatically when set to null or omitted).
     * @return string
     */
    function custom_number_format($number, $decimals = 0, $dec_point = null, $thousands_sep = null) {
        $locale            = setlocale(LC_NUMERIC, 0); // yes, there is no getlocale()
        $def_dec_point     = '.';
        $def_thousands_sep = ',';
        switch (substr($locale, 0, 2)) {
            case 'de':
                $def_dec_point     = ',';
                $def_thousands_sep = '.';
                break;
            case 'fr':
                $def_dec_point     = ',';
                $def_thousands_sep = ' ';
                break;
            case 'en':
            default:
                $def_dec_point     = '.';
                $def_thousands_sep = ',';
        }
        if (!isset($dec_point)) $dec_point = $def_dec_point;
        if (!isset($thousands_sep)) $thousands_sep = $def_thousands_sep;

        return number_format($number, $decimals, $dec_point, $thousands_sep);
    }

    $nr = 1234.56;
    setlocale(LC_NUMERIC, 'de_DE');
    echo custom_number_format($nr, 2);
    # output: 1.234,56

    setlocale(LC_NUMERIC, 'en_US');
    echo custom_number_format($nr, 2);
    # output: 1,234.56

    setlocale(LC_NUMERIC, 'fr_FR');
    echo custom_number_format($nr, 2);
    # output: 1 234,56

    # you still can use whatever you want as dec_point and thousands_sep no matter which locale is set
    setlocale(LC_NUMERIC, 'de_CH');
    echo custom_number_format($nr, 2, '.', "'");
    # output: 1'234.56

As addition to ircmaxell's answer:

If you have a multilingual software and you don't want to pass the dec_point and thousands_sep parameter every time you call number_format you can use a custom function that checks the current locale for determining the current decimal point and thousands separator character like this:

<?php
    /**
     * Calls {@link number_format} with automatically determining dec_point and thousands_app
     * according to the current locale in case they are null or omitted.
     * @param float $number The number being formatted.
     * @param int $decimals [optional] Sets the number of decimal points.
     * @param string|null $dec_point [optional] Decimal point character (determined automatically
     *        when set to null or omitted).
     * @param string|null $thousands_sep [optional] Thousands separator character (determined
     *        automatically when set to null or omitted).
     * @return string
     */
    function custom_number_format($number, $decimals = 0, $dec_point = null, $thousands_sep = null) {
        $locale            = setlocale(LC_NUMERIC, 0); // yes, there is no getlocale()
        $def_dec_point     = '.';
        $def_thousands_sep = ',';
        switch (substr($locale, 0, 2)) {
            case 'de':
                $def_dec_point     = ',';
                $def_thousands_sep = '.';
                break;
            case 'fr':
                $def_dec_point     = ',';
                $def_thousands_sep = ' ';
                break;
            case 'en':
            default:
                $def_dec_point     = '.';
                $def_thousands_sep = ',';
        }
        if (!isset($dec_point)) $dec_point = $def_dec_point;
        if (!isset($thousands_sep)) $thousands_sep = $def_thousands_sep;

        return number_format($number, $decimals, $dec_point, $thousands_sep);
    }

    $nr = 1234.56;
    setlocale(LC_NUMERIC, 'de_DE');
    echo custom_number_format($nr, 2);
    # output: 1.234,56

    setlocale(LC_NUMERIC, 'en_US');
    echo custom_number_format($nr, 2);
    # output: 1,234.56

    setlocale(LC_NUMERIC, 'fr_FR');
    echo custom_number_format($nr, 2);
    # output: 1 234,56

    # you still can use whatever you want as dec_point and thousands_sep no matter which locale is set
    setlocale(LC_NUMERIC, 'de_CH');
    echo custom_number_format($nr, 2, '.', "'");
    # output: 1'234.56
绝不放开 2024-11-11 11:49:59
$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);
$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文