www.webdeveloper.com
Results 1 to 1 of 1

Thread: Need help writing a simple IF clause...

  1. #1
    Join Date
    Jan 2018
    Posts
    1

    Need help writing a simple IF clause...

    Hello guys,

    First of all I'm really new to PHP (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 PHP 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 PHP...


    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.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,760
    BTC as in Bit coin has more than that number of decimals, on my account, depending on what I put in, depends on if I get a number with 11 decimal places or 8 or 4 decimal places... depending on the price (at the time of transaction), the higher its value is, the less you have to spend for the same amount of product. Because BitCoin is very volatile, its value fluctuates too much to be a viable currency until it is stabilized. Until it is stabilized, its going to be volatile and the more decimal points you end up using because of it. In the last month alone its lost over 5,000 ( about $6,500 ) alone despite agencies invested in it hyping up the price which is currently falling.

    Anyway, you need more decimal points At the moment 0.0000005 Btc = 0.01 GBP ( or 1p) 0.000097 Btc = 1

    As for your need to format a number to then either pass a string or return a flaot number is a bit strange, you either want a number of to display a formatted number, using number format to generate a string to return a number is not how you trim any trailing digits, thats what a rounding function is for.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"