'How to set custom price format for woocommerce?

Hi I have problem with woocommerce decimal, i need to display the price with the following format:

  • 0.1

  • 0.10

  • 0.100

After a lot of research i found this code but was not working correctly!

add_filter( 'formatted_woocommerce_price', 'wc_custom_price_format', 10, 5 );

function wc_custom_price_format( $number_format, $price, $decimals, $decimal_separator, $thousand_separator){

$lastnum = $number_format[strlen($number_format)-1];

if ($lastnum <= 0):

   return substr($number_format, 0, -2);
    
elseif ($lastnum == 0):

   return substr($number_format, 0, -1);

else:

    return $number_format;


endif;

}

enter image description here



Solution 1:[1]

<?php

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 );
    $decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
    return $unit . '' . $decimal_separator. $decimal . '';
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maulik patel