Money

It makes the World go round and so we've dedicated a complete object in its honour with some pretty funky methods to help you with multi-currency theme requirements.

The money object is found throughout Evance, as you would expect with an ecommerce platform. Product prices and cart values are automatically cast as Money objects, but you can instantiate a Money object yourself:

Instantiation


// creating a new Money object
var gbp = new Money(1); // use the current locale's currency
var usd = new Money(1, 'USD'); // explicitly set the currency

// from a number
var num = 1.toMoney();

Constructor parameters
value

Type Number

The decimal value.

[currency]

Type String (optional)

The currency of your Money object. When omitted the default currency for the current locale is used.



Properties
prefix

Type String

The left symbol for the currency.

suffix

Type String

The right symbol for the currency.



Methods
convertTo(String:isoCode)

Returns Money

Convert to a new Currency and adjust the value using Evance's internal exchange rate. The new Money object will adopt the correct formatting for the new isoCode Currency.


var num = 1;
var cur = num.toMoney(); // £1.00  if current locale has a currency of GBP
var usd = cur.convertTo('USD'); // $1.60 if the exchange rate is 1.6 USD to 1 GBP
print(usd);
currency([String:isoCode])

Returns Money | Currency

Get or set the current Currency of the Money object. Returns a Currency object is no isoCode parameter is provided. Or, changes the current currency to a new isoCode Currency without adjusting the value. The formatting of the current Money object will alter to the new Currency format. This method returns the existing Money object allowing you to chain methods. 


var num = 1;
var cur = num.toMoney(); // £1.00  if current locale has a currency of GBP
cur.currency('USD'); // $1.00 no exchange takes place
print(cur);
decimal([Number:precision])

Returns String

Returns the decimal value as a new String to preserve decimal places. If precision is not supplied the number of decimal places defaults to the number of decimal places for the currency.


var num = 1;
var cur = num.toMoney(); // £1.00  if current locale has a currency of GBP
print(cur.decimal()); // 1.00
format([Number:precision [, String:decimal [, String:thousands]]])

Returns String

Allows you to output the Money value in a format of your choice. However, unlike the Number object the default values for precision, decimal and thousands are that of the Currency object and as such all are optional. The format method is also the toString method and therefore only needs to used if you wish to alter the format of the Money when outputting it. 


var num = 1;
var cur = num.toMoney(); // £1.00 if current locale has a currency of GBP
print(cur.format(0)); // £1
toNumber()

Returns Number

Returns a new Number representing the decimal value of the Money.

toString()

Returns String

Returns a new String representation of Money using all default values for the Money.format() method.