November Happy Hour will be moved to Thursday December 5th.

Loading...
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

 Rounding totals for different currencies

This topic explains the proper way to round amounts in Commerce orders. Rounding is complex because different currencies use different numbers of decimal places.

The following methods return a value with the correct number of decimals, which is determined by the input currency.

  • Currency.Round()
  • Currency.Percentage()
  • Money.Round()

Do not use Math.Round() for rounding. For example, do not use the following code.

var currentCurrency = “JPY” 
Money itemPrice = new Money(Math.Round(lineItem.ListPrice, 2), currentCurrency);

If you currently use Math.Round, replace it like this.

var currentCurrency = “JPY”
Money itemPrice = new Money(lineItem.ListPrice, currentCurrency).Round();

Calculating and rounding an order total

After rounding, use the rounded value for all further totals. For example, here is the correct way to add a number.

var billingCurrency = order.Currency;
foreach (var item in order.GetAllLineItems())
{
  var costWithoutDiscount = billingCurrency.Round(item.PlacedPrice * item.Quantity);
  item.Properties["costWithoutDiscount"] = costWithoutDiscount;
}

Calculating taxes

To get an order‘s correctly-rounded tax amount, do not use the tax amount of individual items. Instead, follow this example.

var billingCurrency = order.Currency;
var saleTaxesAmount = taxes
.Where(x => x.TaxType == TaxType.SalesTax) .Sum(x => billingCurrency.Percentage(itemPriceWithoutTax, x.Percentage));

In the above code, saleTaxesAmount is returned with a rounded value.

Note: Do not use billingCurrency.Percentage(itemPriceWithoutTax, x.Percentage) in other situations.

Examples

Do you find this information helpful? Please log in to provide feedback.

Last updated: Feb 22, 2016

Recommended reading