I think this is a common requirement, when you've got prices like $5,300.00 most people don't want that trailing .00 thing, whereas if the price is $500.65 they do want the decimals.

We could easily put this as an extra option of the currency settings (even just in code) and then do something like this in commerce_currency_format()

$decimals = (floor($amount) == $amount) ? 0 : $currency['decimals'];

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Status: Active » Needs review

Any reason we can't just direct people to http://drupal.org/project/commerce_extra_price_formatters? I wouldn't make this part of the currency format itself, as it's more of a case-by-case presentation issue.

pcambra’s picture

There's a slightly tiny minor detail in that module, that formatter overrides commerce_currency_format function and it just removes the decimal part no matter what, so both $500.00 and $500.15 would be displayed at $500.

I could patch extra price formatters if you feel that kind of setting doesn't belong to core but then we have the issue that you can't combine formatters so you can't use commerce_extra_price_formatters with commerce_price_savings or commerce_price_table

pcambra’s picture

Also, we have the issue with the order total views handler, which is not a field and we can't change anything there by UI.

rszrama’s picture

Hmm, good point. That area handler uses the "Formatted with components" display formatter, but there's no reason we can't make it configurable.

bgilhome’s picture

I've attached a patch to add a key 'decimals_zero' to the info array for each currency, value is boolean to enable showing/hiding decimals when they are zero.

philsward’s picture

I tried the patch, but it did nothing for me...

Price formatting is VERY important to the psychology of purchasing.

Though the following blogs aren't what I would call "educational", studies have been done on the matter and there's a lot of info out on the web about price formatting. Keeping up with the best practices, no matter how uneducated the users of Drupal Commerce may be, is worth doing. A programmer is more than willing to stay complaint and keep up with best practices on metatags, or HTML5, or PHP despite the users not knowing a thing about those particular best practices, but it doesn't make sense to keep up on making Commerce work as an up-to-date shopping cart from a marketing standpoint? Just throwing it out there...

See:
Remove the Comma When Possible

Avoid Discounts With Precise Numbers

http://www.helpscout.net/blog/pricing-strategies/ - See Point #10

http://m.huffpost.com/us/entry/6182498 - towards the bottom

There's a lot more to prices than just displaying the numbers a certain way on a use-case basis. It needs to be looked at from a marketing standpoint, not a ux or code design standpoint.

If I could code, then sure, I'd go into the template.php and add the custom function pre_process thingy, but I don't code. I can't. Don't have the mind for it. How I can use and manage and navigate Drupal alone, is beyond me but that's the precise reason I USE Drupal: So I can have all of the power without ever touching the code.

szeidler’s picture

The patch is working as expected for me.

@philsward. Did you configured your currency to hide the zero decimals? You need to set the property to false, clear the cache and it should work.

function hook_commerce_currency_info_alter(&$currencies, $langcode) {
  $currencies['YOUR_CURRENCY_CODE']['decimals_zero'] = FALSE;
}
philsward’s picture

thanks @szeidler. I tried the template.php function code, but couldn't get it to work. I ended up manually changing "'decimals_zero' => FALSE," inside the commerce.module file to get it to work.

From what I can tell, what you posted should work fine (USD for currency code) but it just wouldn't do it for me... Either I'm just that clueless or there's a piece missing. :-/

szeidler’s picture

I believe the alter code only works in a custom module, not template.php

philsward’s picture

Take a look at: https://www.drupal.org/project/commerce_price_decimals_formatter

This allows a formatter setting of "Formatted amount with n decimals", and that allows the setting of "Sets zero decimal places when possible" which removes the decimal if .00

Not sure how it will affect other aspects of price calculations, but it does work for a basic price change.