我具有自定义功能,该功能汇总了wordpress数据库中自定义字段的值。
这些值已正确汇总,并显示如下: 1500.3€
如何在小数点后添加零,以便总和显示如下 1500.30€
我想我需要使用number_format()格式化$ sum,但是我不确定100%如何在我的函数中使用number_format。
它应该看起来像这样:
number_format($sum, 2, '.', '');
但是我可以将上面的内容坚持我的功能吗?
自定义功能:
function kd_shortcode_group_progress($atts, $content = null) {
extract( shortcode_atts( array(
'challenge' => '0',
'group' => '1',
), $atts ) );
global $wpdb;
$prefix = $wpdb->prefix;
global $kd_table;
$table = $prefix."postmeta";
$table2 = $prefix."posts";
$sum = 0;
$sql = "SELECT `post_id` FROM `".$table."` WHERE `meta_key` = 'wpcf-haaste_valmis' and `meta_value` = '".$challenge."';";
$people = $wpdb->get_results($sql);
$sql4 = "SELECT `id` as `post_id` FROM `".$table2."` WHERE `post_type` = 'osallistuja' and `post_title` = '".$group."';";
$ids = $wpdb->get_results($sql4);
$pageId = '';
foreach ($ids as $i => $id) {
$people[] = $ids[$i];
}
foreach ($people as $i => $val) {
$pid = $val->post_id;
$sql2 = "SELECT `meta_value` as 'value' FROM `".$table."` WHERE `meta_key` = 'wpcf-keratty' AND `post_id` = '".$pid."';";
$sql3 = "SELECT SUM(`price`) as 'value' FROM `".$kd_table."` WHERE `for` = '".$pid."';";
$dons1 = $wpdb->get_results($sql2);
$dons2 = $wpdb->get_results($sql3);
foreach ($dons1 as $i2 => $val2) {
$sum += $val2->value;
}
foreach ($dons2 as $i2 => $val3) {
$sum += $val3->value;
}
}
return '<div class="groupDonations"><span style="color:#55A228!important;">This group has collected </span><br>'.$sum.'€</span></div>';
}
答案 0 :(得分:1)
您需要使用WordPress number_format_i18n
$formatted = number_format_i18n( 1500.3, 2 ); // 1500.30
number_format
也可以使用,但是它与语言无关
echo number_format(1500.3, 2, '.', ''); // 1500.30
答案 1 :(得分:0)
$sum = 0;
echo number_format($sum, 2, '.', '');
工作正常-实际显示它可能是前端问题? 尝试将其用作字符串,
$sum = "" . number_format($sum, 2, '.', '');