1
Answers
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:
http://drupal.stackexchange.com/questions/76403/round-commerce-price-in-...
the actual altering works for me with this code:
<?php
// 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;
646
647 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 }
653
?>
Is this a sane approach, or will i step into some traps in other places?
Comments
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.
One last follow-up: Take a look at https://www.drupal.org/project/commerce_extra_price_formatters
They have an option to remove decimals from prices. I would take a look at their code.
Thanks.
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.