千位分隔符与打印


Thousands separator with printf

如何在printf中使用千个分隔符(如逗号)

的例子:

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

UPDATE这是我原来的:

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>";
但是我需要逗号

让你的代码漂亮一点,不要全部回显。跳出PHP上下文来做,然后用number_format:

代替printf
</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>

可以使用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

除了ircmaxell的回答:

如果你有一个多语言软件,并且你不想在每次调用number_format时传递dec_point和thousands_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
$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);