Rounding converted currency values

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by ka_ on September 3, 2011 at 7:21am

I have just created a rule that converts a value from one currency to another for Drupal Commerce
The rule is triggered by "Calculating the sell price of a product"
with the condition "Data compare" on [commerce-line-item:commerce-unit-price:currency-code] = "
and actions: "Convert the unit price to a different currency" and "Multiply the unit price by some amount".

So far so good, however I intend to ensure the value is always rounded upwards to the next value without decimals.
As I understand, I might be able to do partly what I want by defining the rounding values of the currency itself, however, I would likely then not be able to force the value to round up or down but only to nearest value. Rounding a value seem like a fairly common task that should be added to the list of Add, subtract, multiply, and divide. I consider if it should be an CEIL/FLOOR/ROUND option with a precession as to whether the number should be rounded to nearest digit of decimal or whole number. (i.e. 0.1, 1.0, or 10.0)

Categories: , ,

Comments

I had exactly the same issue, here's my fix

Posted by James A on October 18, 2012 at 9:45am

I wanted to achieve exactly the same thing as you have done, and came up with exactly the same issues. My fix was as follows:

To apply the currency conversion and then uplift on the exchange rates from central bank I firstly check to see if the selected commerce currency is not the same as the products currency so that I didn't apply an uplift to products with base currencies the same as the chosen currency. For example if I normally sell apples in £ and the buyer wants to pay in £ then no exchange or uplift, but if the want to pay for the apple in $ then the price is converted and a 2% uplift is applied to cover my exchange rate risk. This first element is as follows

OR
"Data comparison"
Parameter: Data to compare: [site:commerce-currency], Data value: EUR
"Data comparison"
Parameter: Data to compare: [commerce-line-item..., Operator: is one of, Data value: GBP, USD
OR
"Data comparison"
Parameter: Parameter: Data to compare: [site:commerce-currency], Data value: GBP
"Data comparison"
Parameter: Data to compare: [commerce-line-item..., Operator: is one of, Data value: EUR, USD
etc

If the currencies are different I then do the convert as you did and multiply the result by 1.02 (adding 2%) to cover me against currency conversion costs.

"Convert the unit price to a different currency"
Parameter: Line item: [commerce_line_item], Currency: [site:commerce-currency]
"Multiply the unit price by some amount"
Parameter: Line item: [commerce-line-item], Amount: 1.02, Price rounding mode: Round the half to the..

To then get my items to always round up I add 49 to the price (equivalent to 49 cents) then divide it by 100 and round and multiply it by 100 again.

"Add an amount to the unit price"
Parameter: Line item: [commerce_line_item], Amount: 49
"Divide the unit price by some amount"
Parameter: Line item: [commerce_line_item], Amount: 100
"Multiply the unit price by some amount"
Parameter: Line item: [commerce_line_item], Amount: 100

I hope this helps someone else, or if someone know a better way they let me know.

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /