Class: Amount

timapi.Amount(value, currency, exponent)

Amount represents an amount of a specific currency.

Overview

Members-Overview

Methods-Overview

Constructor

new Amount(value, currency, exponent)

Create amount for transactions using minor units.
Throws:
  • Will throw error if it's a floating point number. To make sure it's an integer you can either use Math.floor (8.6 -> 8) or Math.round (8.6 -> 9)
  • Will throw error if amount is not representable as a 52bit value. Use Number.isSafeInteger(value) to check wether value is safe or not
  • Will throw error if amount is 0
Example:
  • An amount of CHF 12.30 amount would be 1230 in minor units since CHF has an exponent of 2.
Parameters:
Name Type Description
value Number Amount in minor units as an integer value
currency timapi.constants.Currency Currency of given amount
exponent Number Optional: specific exponent (otherwise defined by currency)

Member-List

currency :timapi.constants.Currency

Currency to use for transaction.

exponent :Number

Exponent.

value :Number

Amount in minor units as integer value.

Methods

getDecimalValue() → {Number}

Get amount-value in major units as floating point value.
Returns:
Amount-value in major units as floating point value

Type: Number

setCurrency(currency)

Set currency to use for transaction. Also sets the exponent. Assign to currency property directly if you want to keep the exponent unchanged.
Parameters:
Name Type Description
currency timapi.constants.Currency Currency

setDecimalValue(value)

Set amount-value in major units as floating point value
Parameters:
Name Type Description
value Number Amount-value in major units as floating point value (e.g. 10.5)
Throws:
  • Will throw error if amount in minor units is not representable as a 52bit value
  • Will throw error if amount is 0

setValue(value)

Set value of amount.
Parameters:
Name Type Description
value Number Value of amount as an integer value
Throws:
  • Will throw error if it's a floating point number. To make sure it's an integer you can either use Math.floor (8.6 -> 8) or Math.round (8.6 -> 9)
  • Will throw error if amount is not representable as a 52bit value. Use Number.isSafeInteger(value) to check wether value is safe or not
  • Will throw error if amount is 0

toString() → {String}

String representation for debugging purpose.
Returns:
String representation for debugging purpose

Type: String

(static) copy(amount) → {timapi.Amount}

Creates a copy of a timapi.Amount object.
Parameters:
Name Type Description
amount timapi.Amount Original
Returns:
Copy of original

Type: timapi.Amount

(static) fromMajorUnits(valueMajor, currency, exponent) → {timapi.Amount}

Create amount from major amount-value.
Parameters:
Name Type Description
valueMajor Number Amount-value in major units as an floating point value
currency timapi.constants.Currency Currency of given amount
exponent Number Optional: specific exponent (otherwise defined by currency)
Throws:
  • Will throw error if amount in minor units is not representable as a 52bit value
  • Will throw error if amount is 0
Returns:
Created amount

Type: timapi.Amount

Example
An amount of CHF 12.30 amount would be 12.3 in major units (1230 in minor units) since CHF has an exponent of 2.