Search code examples
numberspercentagesubtraction

PHP subtract a percentage result


$temp_price_1 = round($price * ((100-$sale_percentage) / 100), 2); $price = $temp_price_1;

im using this code to subtract a percentage from a number, for this example my starting number for $price is 24.99 and the $sale_percentage is 50.0 this result is the $temp_price_1 being 12.5 which is correct, however i need it to be 12.50 so it looks like a correct currency value. How do i add the 0 back on the end.

Many thanks


Solution

  • Check out the number_format function (returns a string):

    number_format($price, 2);
    

    Will return the string "12.50" when $price is set to 12.5