Discussions

Price without the decimal?

Hi

How to setup price without the decimal?
Now is 99.00, I need only 99.

Thank You

Posted: Feb 21, 2012

Comments

nawi on February 22, 2012

Hi all

I found solution.
In commerce.curency.inc (modules/commerce/includes).
Find row with decimals and change number. If you do not have, just add.

cord1 on February 26, 2012

Using 7.x-1.2+10-dev

I find it strange that in the database table "field_data_commerce_price" field:
`commerce_price_amount` int(11) NOT NULL DEFAULT '0' COMMENT 'The price amount.',
is integer.
Should it not be of type decimal?

Creating a product with the amount of 55 or 55.00 this is saved in the database as 5500
The comma shows up correctly (55 shows up as 55,00) in the list in Administration » Store » Products
but not in display, here 55 or 55,00 shows up as 5500

I tried to add the line:
'decimals' => 2,
in code in modules/commerce/includes/commerce.curency.inc
'NOK' => array(
'code' => 'NOK',
'symbol' => 'Nkr',
'name' => t('Norwegian Krone'),
'thousands_separator' => ' ',
'decimals' => 2,
'decimal_separator' => ',',
'numeric_code' => '578',
'minor_unit' => t('Øre'),
'major_unit' => t('Krone'),
),

But that did not help.

Anyone who can help here?

Yuri on March 28, 2012

Ive tested about 4 commerce price formatting modules, but none of them worked for me. if someone has a working method for the product display prices to be whole numbers without decimal/cents, please let me know.
Thanks

rlnorthcutt on June 19, 2012

Without hacking DC core, you can use hook_commerce_currency_info_alter to add/change any of the core currency formatting. You can look at the hook_commerce_currency_info api reference for info on the possible settings per currency code. Here is an example to remove decimals for euros, pounds, and dollars:

<?php
/**
 * Implements hook_commerce_currency_info_alter().
 */
 
function MYMODULE_commerce_currency_info_alter(&$currencies, $langcode) {
 
$currencies['EUR']['decimals'] = 0;
 
$currencies['GBP']['decimals'] = 0;
 
$currencies['USD']['decimals'] = 0;
}
?>

WARNING: If you do this for your default currency, it will have the effect of multiplying all of your prices by 100. So, you will need to go in and change your prices or write a rule to divide them all by 100.

davidwhthomas on August 9, 2012

I wanted to do the same thing and found the same hook solution.

However, setting 'decimals' to 0 will cause issues with the commerce_amount_to_decimal function, e.g 499 becomes 49,900 as mentioned by rlnorthcutt.

I needed to just format the output of the price, and not have to change the prices on hundreds of products.

So, my solution is:

<?php
/**
 * hook_commerce_currency_info_alter
 * Remove decimal places from HKD
 */
function EXAMPLE_commerce_currency_info_alter(&$currencies){
 
// Add format_callback to remove decimal places from all prices in HKD
 
$currencies['HKD']['format_callback'] = 'EXAMPLE_commerce_currency_format';
}

/**
 * Currency format callback
 *
 * Remove decimal places from output
 * while keeping commerce_amount_to_decimal formatting the same
 *
 * See: commerce_currency_format
 */
function EXAMPLE_commerce_currency_format($amount, $currency, $object = NULL, $convert = TRUE) {
 
 
// Format the price as a number, use 0 for no decimal places in output
 
$price = number_format(commerce_currency_round(abs($amount), $currency), 0, $currency['decimal_separator'], $currency['thousands_separator']);

 
// Establish the replacement values to format this price for its currency.
 
$replacements = array(
   
'@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
   
'@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
   
'@price' => $price,
   
'@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
   
'@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
   
'@negative' => $amount < 0 ? '-' : '',
   
'@symbol_spacer' => $currency['symbol_spacer'],
   
'@code_spacer' => $currency['code_spacer'],
  );

  return
trim(t('@code_before@<a href="mailto:code_spacer@negative">code_spacer@negative</a>@<a href="mailto:symbol_before@price">symbol_before@price</a>@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
}

?>

P.S Note this comment input filter has converted the @ stuff in trim() to an email link :-/
Just use the code from commerce.module commerce_currency_format there.

I hope that helps!

Cheers,

DT