In Woocommerce, I\'ve set the number of decimal to 7 on in Woocommerce general settings, so I can display the product price like this $0.0453321>
In my case I needed to keep 3 decimal places "visible" throughout the site but approximate only the grand total to 2 decimal places (with a zero for the third decimal place) because my payment gateway accepted only 2 significant digits after the decimal.
Prices displayed as: 0.336
Taxes: 0.017
But grand total needed to be: 0.350 (instead of 0.353)
I ended up not using the code because it was so horrific but you can say it was a mental excercise:
add_filter( 'wc_get_price_decimals', 'change_prices_decimals', 20, 1 );
function change_prices_decimals( $decimals ){
if( is_cart() || is_checkout() )
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,0);
$length = count($trace);
for ($i = 0; $i < $length; $i++)
{
if($trace[$i]["function"] == "set_total"){
$decimals = 2;
return $decimals;
}
}
}
return $decimals;
}