开发者

Thousands separator with printf

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


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>


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


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


$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜