I am using woocommerce 3.5.3 and Woocommerce All Discounts plugin from Orion. After I am applying the discounts. The prices look like this: €59.90 -> €47.92, €69.90 -> €55.92. etc.
How can I round it to the first decimal digit?
I have already tried a few solutions but none work. For example:
function round_price_product( $price ){
// Return rounded price
return round( $price );
}
If we split the price in integers and fractions then we can round the fractions and use str_pad to make sure we don't loose a digit (47.9 instead of 47.90)
Because we divide the fraction with 10 we get a new fraction that can be rounded with zero precision.
Then multiply it with 10 to get it "back" as originally.
function round_price_product( $price ){
// Return rounded price
$parts = explode(".", $price);
$parts[1] = round($parts[1]/10,0)*10;
if($parts[1] == 100) { // round up to next integer
$parts[0]++;
$parts[1] = 0;
}
return $parts[0] . "." . str_pad($parts[1], 2, 0, STR_PAD_RIGHT);
}
echo round_price_product("47.92"); //47.90
// 47.95 -> 48.00
// 47.02 -> 47.00