php部门结果为零(更新 - 问题在其他地方)

UPDATE - the problem was actually completely out of the rounding function. It seems as though $price (used in woo commerce) is a string and for some reason I can't use it in a calculation. If I simply return $price, no problem. All of this was fine when I was simply returning the value of the other function.

 function xa_only_sale_price($price, $product)
{    
error_log ("price in the beginning is " . $price);
if(!is_cart() && !is_checkout() && !is_ajax()){
    if ($product->is_type('simple') || $product->is_type('variation')) {
        $price = regularPriceHTML_for_simple_and_variation_product($price, $product);
        $val = (float)$price; 
        error_log( "ceiling = " . ceil($val* 2) / 2);  // 0 printed to log
       return ceil($val* 2) / 2;  this returns 0

       // return roundNum(regularPriceHTML_for_simple_and_variation_product($price, $product));
    } 
}
  error_log ("price before call is " . $price);      // this returns 0
  // return roundNum($price);   //this is never in use      

} --------------- original post-----------------------------

I am new to php - thank you for all of the help in advance. I am assuming that this issue has something to do with data types but I haven't been able to figure this one out.

In this example $num is the price of a woocommerce product. If I simply return $num I see the price that I am expecting to see. I am simply trying to round the value in this function (I simplified the function for the sake of the question).

function roundNum($num){ 
    $nearest = 0.50;
    return ($num / $nearest) ;

This returns 0 to the browser. However, forcing the value of $num results in a valid calculation and return.

function roundNum($num){ 
    $num = 100.0;
    $nearest = 0.50;
   return ($num / $nearest) ;

The simplest solution is to typecast your input. A small example in your case is:

function roundNum($num){ 
    $nearest = 0.50;
    $result = (float) $num/ (float) $nearest;
    return $result;
}

Read more about typecasting here

EDIT:

As it turns out your $num is a string. You can change this to a type float and make your calculations, like so:

$num = "48.2";
$float = (float)$num;
echo ceil($float * 2) / 2;

In this example your number is always rounded up by 0.5, so in this case to 48.5

Demo