'Manage independently the decimal number of the price of each product [duplicate]

I develop an online store with woocommerce and I would like to display the price of the product with 3 decimals when the price contains 3 (example 1). But when the last number of the price is 0, I want to display only 2 decimals (example 2). Do you know how to do this please ?

I found a filter, but it affects all the prices at the same time :

add_filter( 'wc_get_price_decimals' , 'custom_price_decimals', 20, 1 );
function custom_price_decimals( $decimals ) {
  if( is_product() || is_shop() || is_product_category() || is_product_tag() || is_cart() || is_checkout() ){
    $decimals = 3;
    return $decimals;
  }
}

Thank you for your help :)

Example 1

example 1
(source: noelshack.com)

Example 2

example 2
(source: noelshack.com)



Solution 1:[1]

You can do that:

$decimals=strlen(ltrim(($float - floor($float)),"0."));

It returns numbers after dot without zeros

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 Mr Tea