如何在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>";
但我需要逗号
答案 0 :(得分:9)
让你的代码漂亮,不要回应所有。只需打破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>
答案 1 :(得分:7)
您可以使用number_format函数。
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
答案 2 :(得分:0)
$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);