Math functions on a variable of type currency

I was struggling to do a math function on a variable set as type currency. I know that math functions on currency are coming in release 17.15, but I was looking for away to achieve this in the interim. I was about to do a convoluted hack of converting the currency to a string, string to number, then multiply the number, and then take it back to a currency, but found out that currency is an object that contains an amount attribute that is the numerical value of the currency. So if I have a currency variable called ‘myCurrency’, i can do math on it by addressing myCurrency.amount * whatever.


Welcome, and great call out! When using the currency type, the object will looks something like this:

  "amount": 12300,
  "code": "USD",
  "precision": 2

Which means you can use the amount property and do any math accordingly.

1 Like

While this was a good workaround in older versions, It’s worth noting that as of 17.15, using .amount directly is not recommended. (NOTE: All versions below 17.15 are no longer supported, and attempting to use this workaround in 17.15+ will throw an error diagnostic in Studio).

The Airkit platform treats currencies differently from numbers, and using the built-in operators will give you more protections against potential app bugs. For example, if you had:

curr1 = { "amount": 10000, "code": "JPY", precision: 0 }
curr2 = { "amount": 100, "code": "USD", precision: 2 }

If you attempted to add using only amount, such as: CURRENCY(curr1.amount + curr2.amount, "USD"), then you’d end up with $10100. However ¥10000 is only about $78.

It’s an uncommon use case, but the Airkit platform will give you an error if you attempt to add/subtract currencies of different types to ensure you don’t end up with subtle bugs with your calculations. You can never be too safe when it comes to your customer’s money! :slight_smile: