Tags/topics: 
1
Answers
Vote up!
0
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:
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?

Asked by: sirtet
on September 9, 2014

1 Answer

Vote up!
0
Vote down!

Looks like a very sane approach to me.

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

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.

- Josh Miller on September 19, 2014

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.

- sirtet on September 19, 2014