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
amount Number
The decimal value.
currency String (optional)

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



Methods
amount() Number
Returns the exact amount of money (not rounded).
convertTo(String:isoCode) 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]) Money | Currency

Get or set the current Currency of the Money object. 

Gets a Currency object if no isoCode parameter is provided. 

Sets the currency to the supplied isoCode without adjusting the amount. The formatting of the current Money object will alter to the new Currency format. Returns the existing Money object.


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]) 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([precision [, decimal [, thousands]]]) 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. 

Parameters

precision Number
Optional. The number of decimal places to render the amount in. The default precision for the currency will be used if not supplied.

decimal String
Optional. The character to use as a decimal point. The default character for the currency will be used if not supplied.

thousands String
Optional. The character to use as a thousand point separator. The default character for the currency will be used if not supplied.


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

Alias of symbolLeft() (see below).

suffix() String

Alias of symbolRight() (see below).

symbolLeft() String

The currency symbol usually placed before the decimal value.

symbolRight() String

The currency symbol usually placed after the decimal value.

toNumber() Number

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

toString() String

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