PHP中的数字格式不正确
我正在尝试修改Wordpress主题中的一些PHP,我没有创建,开发人员也没有太多帮助 我正在为一个客户开发一个网站,展示几个房产清单及其相关价格。当我在编辑器中输入数字时,它会去掉所有的小数。所以如果我输入16.50,它会立即变成1650。我希望它能显示2位小数。我做了一些研究,但我对PHP的知识还不够丰富,不足以让它有意义。开发人员指示我将以下代码调整为$price_meta,但只需在数字上添加2位小数,即可显示1650.00。似乎小数点在输入后会立即被删除。我还需要做些什么才能让它正确地显示出来吗PHP中的数字格式不正确,php,wordpress,number-formatting,Php,Wordpress,Number Formatting,我正在尝试修改Wordpress主题中的一些PHP,我没有创建,开发人员也没有太多帮助 我正在为一个客户开发一个网站,展示几个房产清单及其相关价格。当我在编辑器中输入数字时,它会去掉所有的小数。所以如果我输入16.50,它会立即变成1650。我希望它能显示2位小数。我做了一些研究,但我对PHP的知识还不够丰富,不足以让它有意义。开发人员指示我将以下代码调整为$price_meta,但只需在数字上添加2位小数,即可显示1650.00。似乎小数点在输入后会立即被删除。我还需要做些什么才能让它正确地显
if(!function_exists('ct_listing_price')) {
function ct_listing_price() {
global $post;
global $ct_options;
$ct_currency_placement = $ct_options['ct_currency_placement'];
$price_prefix = get_post_meta(get_the_ID(), '_ct_price_prefix', true);
$price_postfix = get_post_meta(get_the_ID(), '_ct_price_postfix', true);
$price_meta = get_post_meta(get_the_ID(), '_ct_price', true);
$price_meta= preg_replace('/[\$,]/', '', $price_meta);
if($ct_currency_placement == 'after') {
if(!empty($price_prefix)) {
echo "<span class='listing-price-prefix'>";
echo esc_html($price_prefix) . ' ';
echo '</span>';
}
if(!empty($price_meta)) {
echo "<span class='listing-price'>";
echo number_format($price_meta, 2);
ct_currency();
echo '</span>';
}
if(!empty($price_postfix)) {
echo "<span class='listing-price-postfix'>";
echo ' ' . esc_html($price_postfix) . ' ';
echo '</span>';
}
} else {
if(!empty($price_prefix)) {
echo esc_html($price_prefix) . ' ';
}
if(!empty($price_meta)) {
echo "<span class='listing-price'>";
ct_currency();
echo number_format($price_meta, 2);
echo '</span>';
}
if(!empty($price_postfix)) {
echo ' ' . esc_html($price_postfix);
}
}
}
如果(!function\u存在('ct\u清单\u价格')){
函数ct_清单_价格(){
全球$员额;
全球$ct_选项;
$ct_货币_位置=$ct_选项['ct_货币_位置];
$price_prefix=get_post_meta(get_ID(),'u ct_price_prefix',true);
$price_postfix=get_post_meta(get_ID(),'u ct_price_postfix',true);
$price_meta=get_post_meta(get_ID(),'u ct_price',true);
$price\u meta=preg\u replace('/[\$,]/','$price\u meta);
如果($ct\U货币\U位置=='after'){
如果(!空($price_前缀)){
回声“;
echo esc_html($price_前缀)。“”;
回声';
}
如果(!空($price_meta)){
回声“;
回音号码格式($price\u meta,2);
货币();
回声';
}
如果(!空($price_postfix)){
回声“;
echo'.esc_html($price_postfix)。'';
回声';
}
}否则{
如果(!空($price_前缀)){
echo esc_html($price_前缀)。“”;
}
如果(!空($price_meta)){
回声“;
货币();
回音号码格式($price\u meta,2);
回声';
}
如果(!空($price_postfix)){
echo'.esc_html($price_postfix);
}
}
}
我对PHP的了解非常有限,因此,如果您知道如何帮助我,请尽可能用简单和规范的术语进行解释。尝试数字格式($price\u meta,2,“,”);这应该输出16.50根据国际格式,可能有两种选择 如果使用
,
作为十进制分隔符,则函数为:
number\u格式($price\u meta,2',','');
如果要求千位分隔符为
,只需将其添加到前面的函数中,如下所示:
number\u格式($price\u meta,2',',');
另一方面,如果需要使用
作为十进制分隔符,则必须切换:
number\u格式($price\u meta,2,'.','');
使用,
number\u格式($price\u meta,2,,,,);
我尝试了此解决方案,但只能返回1650.00而不是16.50。谢谢,不过我尝试了此解决方案,但只能返回1650.00而不是16.50。