Php 带printf的千位分隔符
如何在Php 带printf的千位分隔符,php,printf,number-formatting,Php,Printf,Number Formatting,如何在printf中使用千位分隔符(如逗号) 例如: printf("<td class='number'>%d</td>", $totals['Sold']); //need thousand separated printf("<td class='number'>%.2f</td>", $totals['Fees']); //need thousand separated printf(“%d”,总计$totals['sall'])//需
printf
中使用千位分隔符(如逗号)
例如:
printf("<td class='number'>%d</td>", $totals['Sold']); //need thousand separated
printf("<td class='number'>%.2f</td>", $totals['Fees']); //need thousand separated
printf(“%d”,总计$totals['sall'])//需要千人分开吗
printf(“%.2f”,总计$['费用])//需要千人分开吗
更新这是我最初拥有的:
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>";
foreach(数组_键($totals)作为$key){
如果(是数字($totals[$key])){
$totals[$key]=数字格式($totals[$key],2);
}
}
回声“
";
回声“总计:”;
echo“{$totals['bunded']}”;
echo“{$totals['salled']}”;
echo“{$totals['Fees']}”;
echo“{$totals['Realized']}”;
echo“{$totals['Net']}”;
echo“{$totals['EOD Price']}”;
回声“
";
我希望它是这样的:
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>";
echo”
";
回声“总计:”;
printf(“%d”,合计$totals['bunded']);
printf(“%d”,$totals['sall']);
printf(“%.2f”,总计$['费用]);
printf(“%.2f”,$totals[“已实现]);
printf(“%.2f”,$totals['Net']);
printf(“%.2f”,总计美元['EOD价格]);
回声“
";
但是我需要逗号,你可以使用数字格式功能
$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
使代码美观,不要重复所有内容。只需中断PHP上下文即可完成此操作,然后使用number_格式而不是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>
总数
...
作为对ircmaxell答案的补充:
如果您有一个多语言软件,并且不希望每次调用number\u format
时都传递dec\u point和数千\u 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
这是什么意思?逗号不是数字,因此使用%d本质上是错误的。我想你要找的是解析一个数字。@Zirak,我想有些数字是带逗号的整数,有些是浮点数。这可能是最好的方法。为什么我没有想到这一点?一个小小的改变是少打字(虽然有人说它不太可读,所以请选择)是做一些像<代码>
这与短\u打开\u标记不同,因此如果禁用了该标记,请不要担心。