Monetary amounts in Gosu

In Gosu, monetary amounts are managed by the MonetaryAmount data type. Like other numeric values, MonetaryAmount objects can be used to perform standard mathematical calculations.

A MonetaryAmount value is initialized when it is constructed. There are two constructors:

MonetaryAmount( BigDecimal amount, Currency currency )
MonetaryAmount( String value )

You can retrieve the default currency by calling the gw.api.util.CurrencyUtil.getDefaultCurrency method. The method returns a Currency object that matches the setting of the config.xml parameter DefaultApplicationCurrency. For other useful CurrencyUtil methods, refer to the Gosu API Reference.

The format of the value string for the second constructor is "Amount CurrencyCode". A space character separates the two parameters. For example:

MonetaryAmount myBalance( "123.45 USD" )

A MonetaryAmount object can also be created from a BigDecimal object by calling the BigDecimal extension method ofCurrency or ofDefaultCurrency. The ofCurrency method accepts a Currency type argument. The ofDefaultCurrency method uses the default currency specified in config.xml. For example:

var baseBigD         = new java.math.BigDecimal( "123.45" )
var monetaryAmount01 = baseBigD.ofCurrency( Currency.TC_USD )
var monetaryAmount02 = baseBigD.ofDefaultCurrency()

For additional information about the MonetaryAmount data type, refer to the Gosu API Reference.

See also