千位分隔符printf
问题描述:
千位分隔符(如逗号)在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>";
但我需要逗号
答
' 这与short_open_tags不一样,所以不要担心,如果你有禁用。 –
2015-12-22 10:56:40
漂亮使您的代码,不附和这一切。刚刚打破了PHP方面的做,然后代替了printf的使用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>
+0
这可能是最好的方法。为什么我没有想到这个? – Neal 2011-05-03 18:42:51
+1
稍微改变一下少一点的打字(尽管有人说它的可读性较差,所以要做出选择)是做类似... '
答
$totals['Sold']=number_format($totals['Sold']);
$totals['Fees']=number_format($totals['Fees']);
答
您可以使用number_format函数。
string number_format (float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',')
答
至于除了ircmaxell的回答是:
如果你有一个多语种的软件,你不希望通过每次调用时dec_point和thousands_sep参数number_format
您可以使用检查当前的语言环境来判断当前小数点和千位分隔符字符像这样的自定义函数:
<?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 2011-05-03 18:33:12
@Zirak,我想要一些数字用逗号整数,有些浮点数 – Neal 2011-05-03 18:35:35