How to perform calculations on currency amounts – XpressDox

Performing calculations on currency amounts

Probably the most accurate way to perform arithmetic on money values in XpressDox is to reduce all currency to integer values (e.g. Dollars to cents, Pounds to pence, etc), perform the arithmetic and then render the value back to the currency value.  The aim is to reduce the possibility of rounding errors creeping in, and also to overcome at least some of the issues covered in the article Handling calculations and Regional Settings in XpressDox.

 

 

Example:

Suppose you have retrieved a value of $1234.56 from a database and this is in the data element «Price».  The snippet

 

The price in cents is «CurrencyToCents(Price)»

becomes: The price in cents is: 123456

 

Now for some arithmetic:

«SetV('Total',0)»
«SetV('PriceInCents',CurrencyToCents(Price))»
«IncrementV('Total',GetV('PriceInCents'))»[repeated a few times in the real world]

Then the result could be rendered back to your Currency with:

«CentsToCurrency(GetV('Total'),'#,0.00')»

or

«CentsToCurrency(GetV('Total'),'#,0.00','fr')»

In other words, it is not necessary to first convert the cents to currency and then apply FormatNumber – those two operations can be combined in one as above.

These functions are available in XpressDox Version 4.3.8 and higher.