/    Sign up×
Community /Pin to ProfileBookmark

Help with a simple IF clause please, I’m new to PHP

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.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Moonito spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...