[wd_asp elements=’search’ ratio=’100%’ id=1]

Show Number as Decimal – Else show first number only

15th March 2019

Php - Strings

php codehaven

Depending if your number has a half like 1.5, 4.5 by checking if the last digit is a zero or not, then it will either show the full number with a decimal showing (1.5 and 4.5) or otherwise it will only show the first number, as it does not need an empty zero. It can show the integer number (1 or 4) instead of (1.0 and 4.0).

There are other ways of doing this, buts its also a good lesson in functions, and can see how it is returned to the function at the bottom.


a".$amount = substr_replace($original_amount ,"",-2);
}
else { //show all of number as its 1.5
return "
b".$original_amount;
}
}

// start
$amount1 = "1.5";
$myamount = amount($amount1);
echo $myamount."h";
?>