Need help to make an if clause to check if decimals are zeroes or not

by Moonito   Last Updated January 13, 2018 00:08 AM

First of all I'm really new to Wordpress (know a little of code but not web). I'm making a site with an Apptheme's theme called classipress for classifieds. My page let's you post and AD in btc or USD but the problem is that for btc I need several decimals (around 5) and with USD it just doesn't feel good seeing 5 decimal (specially five zeroes)... I'd like to know if someone could help me with an IF clause since I read some manuals on wordpress and still can't figure out how to make it in this language.

the working code that always shows the 5 decimals is:

function cp_price_format( $price ) { global $cp_options;

if ( is_numeric( $price ) ) {
    $decimals = ( $cp_options->hide_decimals || $price == 0 ) ? 0 : 5;
    $decimal_separator = $cp_options->decimal_separator;
    $thousands_separator = $cp_options->thousands_separator;

    $price = number_format( $price, $decimals, $decimal_separator, $thousands_separator );
}

return $price;

}

If I were to change the return $price for return floatval($price); it will ALWAYS erase the decimals, I need to only erase decimals if they are only zeroes.

I tried doing something like this:

function cp_price_format( $price ) { global $cp_options;

if ( is_numeric( $price ) ) {
    $decimals = ( $cp_options->hide_decimals || $price == 0 ) ? 0 : 5;
    $decimal_separator = $cp_options->decimal_separator;
    $thousands_separator = $cp_options->thousands_separator;

    $price = number_format( $price, $decimals, $decimal_separator, $thousands_separator );
}

if ($decimals ==0){ return floatval($price);}
else {return $price;}

}

But it didn't work at all... I guess its a problem with the {} or the commas, but can't figure it out just starting with wordpress...

I'd really appreciate if someone could do the IF clause for me so I can make it work and learn how to do it propperly.

Thank you very much for taking the time to read this.

Tags : templates


Related Questions


How to add new CSS file to new PHP file

Updated April 15, 2015 20:03 PM


Get name of the current template file

Updated July 24, 2015 13:03 PM

Stuck with a message across all my pages?

Updated March 15, 2016 08:03 AM

Creating a template - simple as copy paste?

Updated May 08, 2016 08:03 AM