Vote up!
Vote down!

How to alter a 12.00 price to 12.- format?

At least in Switzerland it is quite common to have a dash (seldomly two) instead of double zero on prices without a fraction value.

I think what i need is a custom hook_commerce_currency_info_alter, as described here:

the actual altering works for me with this code:

// Format the price as a number.
643   $price_rounded = commerce_currency_round(abs($amount), $currency);
644   $price_int = intval($price_rounded);
645   $price_fraction = $price_rounded - $price_int;
if ($price_int < $price_rounded){
648      $price = number_format($price_rounded, 2, $currency['decimal_separator'], $currency['thousands_separator']);
649   }
650   else {
651      $price = number_format($price_rounded, 0, $currency['decimal_separator'], $currency['thousands_separator']) . '.-';
652   }

Is this a sane approach, or will i step into some traps in other places?

Asked by: sirtet
on September 9, 2014

1 Answer

Vote up!
Vote down!

Looks like a very sane approach to me.

Josh Miller
Answer by: Josh Miller
Posted: Sep 19, 2014


On second thought, this seems a bit heavy handed. I would recommend you simply set the price to the intval() and then add a suffix ".-" so as to make sure everything is still a number.

- Josh Miller on September 19, 2014

I had a look at commerce_extra_price_formatters before (not the code), and same as your intval() approach, it would affect all prices, regardless if they have decimals or not.
But 12.35 should still be output correctly as 12.35, ONLY 12.00 should be 12.-, hence my if (... statements.

- sirtet on September 19, 2014