One of the stores I manage is currently facing a bug that is not solved yet by WooCommerce, regarding rounding prices introduced without tax. So far one of the temp fixes that work is changing the whole store to 4 decimals to improve accuracy. Now the problem is that some new clients seem to believe our prices are in another currency other than USD because of the 6 digits that get presented (19.998). I'm wondering if there is a way to format the output of woocommerce_get_price_html using something like number_format. I'm a bit lost because the output of this is escaped html to style the price, so something like this

add_filter( 'woocommerce_get_price_html', 'rounded_price_html', 100, 2 );
function rounded_price_html( $price, $product ){
    return number_format( $price, 2, ',', '.');

would not work. So basically what I'm asking is, is there any way I can format the price of this function without interfering with the HTML? This should only affect the front end as we don't care about the four digits in the back. Creating something using JS is not an option as our theme is AJAX based and that would just create more work

3 Answers 3


print the number only with two decimal numbers.

$backend_price = 2.3456;
echo sprintf('%.2f', $backend_price);
  • I don't think the formatting is the problem, it's doing it in a woocommerce_get_price_html filter without changing the other generated HTML.
    – Rup
    Jul 30, 2021 at 16:25
  • Exactly, I just want to change the price format, but not the wrapping html
    – Jay
    Jul 30, 2021 at 23:28

Even if you set the number of decimals in the backend you can still explicitly override it via wp_get_price_decimals filter hook.

 * Return the number of decimals after the decimal point.
 * @params int $decimals The number of decimals. Defaults to get_option( 'woocommerce_price_num_decimals', 2 )
 * @return int
function wp123_custom_decimals( $decimals ) {
   return 2; // The number of decimals.
add_filter( 'wc_get_price_decimals', 'wp123_custom_decimals', 10, 1 );
  • This would simply override the option on WooCommerce's settings, right? That wouldn't work, as in order to fix the tax problem I need to set my site to 3-4 decimals, I only care about formatting the frontend
    – Jay
    Jul 30, 2021 at 23:29

Set the store to 3 decimal places and then use this:

add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    $three_dp = $price - intval( $price );
    $two_dp = ( number_format($three_dp,2) * 100);
    if($two_dp == 100) {
        $two_dp = 0;
    $decimal = sprintf( '%02d', ( $two_dp ) );
    return $unit . '.' . $decimal ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.