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

  • Author
    Posts
  • #3064182

    Hello guys,

    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.

    #3064310

    timethief
    Member

    I’m making a site with an Apptheme’s theme called classipress for classifieds.

    That themes does not exist here at WordPress.COM. Unfortunately you failed to post the most important thing for getting accurate support and it is the URL of the site in question.

    To save you time you can type the actual URL of the site you are referring to into http://whoishostingthis.com to determine who hosts the site so you know where to post for support.

    We provide support only for sites hosted on WordPress.COM servers. You could be posting to the wrong support forum. To be clear we do not provide support for local installs of WordPress.ORG software, or for WordPress.ORG software installs on paid hosting, linked to WordPress.COM accounts with the Jetpack plugin so they display on the My Sites WordPress.COM account page.

    WordPress.COM and WordPress.ORG are completely separate and have different username accounts, logins, features, run different versions of some themes with the same names, and have separate support documentation and separate support forums. Read the differences here http://en.support.wordpress.com/com-vs-org/

    The wordpress.ORG support forum is at http://wordpress.org/support. The wordpress.ORG login link is here https://login.wordpress.org/ If you do not have an account yet then click Create an account https://login.wordpress.org/register/ and if you have lost an account password click Lost password? https://login.wordpress.org/lostpassword/
    WordPress.org support docs are at https://codex.wordpress.org/Main_Page
    See also https://apps.wordpress.org/support/ for app support.

The topic ‘Need help to make an if clause to check if decimals are zeroes or not’ is closed to new replies.