I am working on a WordPress website for a client to show several property listings and their associated prices. When I enter the number in the metabox created by the developer it strips out all decimals. So that if I enter 16.50 it immediately turns into 1650 when the page is saved. I want it to be able to show 2 decimals. I’ve done a bit of research but I’m not knowledgeable enough in PHP/Wordpress for it to make sense. The developer directed me to adjusting the following code for $price_meta however that simply adds 2 decimals onto the number to show 1650.00 and not 16.50 like I want. It seems like the decimal is immediately stripped upon entering it. I think this is a metabox/wordpress issue… I feel as though somewhere there is something defined that strips out all decimals from this entry field but I’m not sure how to counteract it.

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);
    }
}
}

I have very limited knowledge of PHP so if you have an idea how to help please explain in as simple and prescriptive terms as possible.

Read more here: Decimal stripped from metabox number


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: