Money

final class Money extends Quantity[Money]

Represents a quantity of Money.

Represents a quantity of Money.

Money is similar to other quantities in that it represents an amount of something - purchasing power - and it is measured in units - currencies.

The main difference is that the conversion rate between currencies can not be certain at compile. (In fact it may not always be so easy to know them at runtime as well.)

To address this diversion from the way most other quantities work, Money overrides several of the standard methods and operators to ensure one of two rules is followed:

  1. this and that are in the same currency, or
  2. there is in an implicit MoneyContext in scope (which may or may not have the applicable exchange rate)

Methods and operations applied to moneys of different currencies may throw a NoSuchExchangeRateException if the implicit MoneyContext does not contain the Rate(s) necessary to perform the conversion.

The defaultMoneyContext includes USD as the default currency, a list of ~20 other currencies and NO exchange rates

Value Params
amount

the amount of money

currency

the currency in which the money is denominated

Authors

garyKeorkunian

Since

0.1

Companion
object
trait Ordered[Money]
trait Comparable[Money]
trait Serializable
class Object
trait Matchable
class Any

Value members

Concrete methods

def !=#(that: Money)(moneyContext: MoneyContext): Boolean
def %(that: BigDecimal): Money

Integer divides this money by that BigDecimal and returns the remainder

Integer divides this money by that BigDecimal and returns the remainder

Value Params
that

BigDecimal

Returns

Money

def *(that: BigDecimal): Money
override def *(that: Double): Money
Definition Classes

Multiplies this money by that squants.market.CurrencyExchangeRate and returns the equal value in the other currency.

Multiplies this money by that squants.market.CurrencyExchangeRate and returns the equal value in the other currency.

Delegates to CurrencyExchangeRate * Money

Value Params
that

BigDecimal

def +(that: Money)(context: MoneyContext): Money

moneyPlus *

moneyPlus *

def -(that: Money)(context: MoneyContext): Money

moneyMinus *

moneyMinus *

toThe

toThe

def /(that: BigDecimal): Money
override def /(that: Double): Money
Definition Classes
def /(that: Money)(context: MoneyContext): BigDecimal
def /%(that: BigDecimal): (Money, Money)

Integer divides this money by that BigDecimal and returns the quotient and the remainder

Integer divides this money by that BigDecimal and returns the quotient and the remainder

Value Params
that

BigDecimal

Returns

(Money, Money)

def <#(that: Money)(moneyContext: MoneyContext): Boolean
def <=#(that: Money)(moneyContext: MoneyContext): Boolean
def ==#(that: Money)(moneyContext: MoneyContext): Boolean
def >#(that: Money)(moneyContext: MoneyContext): Boolean
def >=#(that: Money)(moneyContext: MoneyContext): Boolean
override def compare(that: Money): Int

Override for Quantity.compare to only work on Moneys of like Currency

Override for Quantity.compare to only work on Moneys of like Currency

Value Params
that

Money

Returns

Int

Definition Classes
Quantity -> Ordered
def divide(that: BigDecimal): Money

Divides this money by that BigDecimal and returns a new Money

Divides this money by that BigDecimal and returns a new Money

Value Params
that

BigDecimal

Returns

Money

override def divide(that: Double): Money

Overrides Quantity.divide to ensure BigDecimal math is performed

Overrides Quantity.divide to ensure BigDecimal math is performed

Value Params
that

Double

Returns

Quantity

Definition Classes
override def divide(that: Money): Double

Override for Quantity.divide to only work on Moneys of like Currency Cross currency subtractions should use moneyMinus

Override for Quantity.divide to only work on Moneys of like Currency Cross currency subtractions should use moneyMinus

Value Params
that

Money

Returns

Double

Definition Classes
override def equals(that: Any): Boolean

Override for Quantity.equal to only match Moneys of like Currency

Override for Quantity.equal to only match Moneys of like Currency

Value Params
that

Money must be of matching value and unit

Definition Classes
Quantity -> Any
override def hashCode: Int

Override for Quantity.hashCode because Money doesn't contain a primary unit

Override for Quantity.hashCode because Money doesn't contain a primary unit

Definition Classes
Quantity -> Any
def in(unit: Currency)(context: MoneyContext): Money

Reboxes this Money value in a Money in the given Currency

Reboxes this Money value in a Money in the given Currency

Value Params
context

MoneyContext required for cross currency operations

unit

Currency

Returns

Money

Throws
NoSuchExchangeRateException

when no exchange rate is available

def mapAmount(f: BigDecimal => BigDecimal): Money

Applies a function to the underlying amount of the Money, returning a Money in the same Currency

Applies a function to the underlying amount of the Money, returning a Money in the same Currency

Value Params
f

BigDecimal => BigDecimal function

Returns

Money

override def max(that: Money): Money

Override for Quantity.max to only work on Moneys of like Currency

Override for Quantity.max to only work on Moneys of like Currency

Value Params
that

Money

Returns

Int

Definition Classes
override def min(that: Money): Money

Override for Quantity.max to only work on Moneys of like Currency

Override for Quantity.max to only work on Moneys of like Currency

Value Params
that

Quantity

Returns

Int

Definition Classes
override def minus(that: Money): Money

Override Quantity.minus to only work on like currencies Cross currency subtractions should use moneyMinus

Override Quantity.minus to only work on like currencies Cross currency subtractions should use moneyMinus

Value Params
that

Money

Returns

Money

Throws
scala.UnsupportedOperationException

when attempted on cross currencies

Definition Classes
def moneyCompare(that: Money)(moneyContext: MoneyContext): Int

Supports compare operation on Moneys of dislike Currency

Supports compare operation on Moneys of dislike Currency

Value Params
moneyContext

MoneyContext

that

Money

def moneyDivide(that: Money)(context: MoneyContext): BigDecimal

Divides this money by that money and returns the ratio between the converted amounts

Divides this money by that money and returns the ratio between the converted amounts

Value Params
context

MoneyContext

that

Money

def moneyEquals(that: Money)(moneyContext: MoneyContext): Boolean

Supports equality comparisons on Moneys of dislike Currency

Supports equality comparisons on Moneys of dislike Currency

Value Params
moneyContext

MoneyContext

that

Money

def moneyMax(that: Money)(moneyContext: MoneyContext): Money

Supports max operation on Moneys of dislike Currency

Supports max operation on Moneys of dislike Currency

Value Params
moneyContext

MoneyContext

that

Money

def moneyMin(that: Money)(moneyContext: MoneyContext): Money

Supports min operation on Moneys of dislike Currency

Supports min operation on Moneys of dislike Currency

Value Params
moneyContext

MoneyContext

that

Money

def moneyMinus(that: Money)(context: MoneyContext): Money

Subtracts that Money from this Money converted to this.currency via context

Subtracts that Money from this Money converted to this.currency via context

Value Params
context

MoneyContext required for cross currency operations

that

Money

Returns

Money

Throws
NoSuchExchangeRateException

when no exchange rate is available

def moneyNotEquals(that: Money)(moneyContext: MoneyContext): Boolean

Supports non-equality comparisons on Moneys of dislike Currency

Supports non-equality comparisons on Moneys of dislike Currency

Value Params
moneyContext

MoneyContext

that

Money

def moneyPlus(that: Money)(context: MoneyContext): Money

Adds this Money to that Money converted to this.currency via context

Adds this Money to that Money converted to this.currency via context

Value Params
context

MoneyContext required for cross currency operations

that

Money

Returns

Money

Throws
NoSuchExchangeRateException

when no exchange rate is available

override def plus(that: Money): Money

Overrides Quantity.plus to only work on like currencies. Cross currency additions should use moneyPlus

Overrides Quantity.plus to only work on like currencies. Cross currency additions should use moneyPlus

Value Params
that

Money

Returns

Money

Throws
scala.UnsupportedOperationException

when attempted on cross currencies

Definition Classes
override def rounded(scale: Int, mode: RoundingMode): Money

Returns a Money rounded using scale and mode.

Returns a Money rounded using scale and mode.

Value Params
mode

RoundingMode - defaults to HALF_EVEN

scale

Int - scale of the Money to be returned

Returns

Quantity

Definition Classes
def times(that: BigDecimal): Money

Multiplies this money by that BigDecimal and returns a new Money

Multiplies this money by that BigDecimal and returns a new Money

Value Params
that

BigDecimal

Returns

Money

override def times(that: Double): Money

Overrides Quantity.times to ensure BigDecimal math is performed

Overrides Quantity.times to ensure BigDecimal math is performed

Value Params
that

Double

Returns

Quantity

Definition Classes
def to(unit: Currency)(context: MoneyContext): BigDecimal

Convert this Money to a Double representing the currency unit

Convert this Money to a Double representing the currency unit

Value Params
context

MoneyContext required for cross currency operations

unit

Currency

Returns

Double

Throws
NoSuchExchangeRateException

when no exchange rate is available

def toFormattedString: String

Returns a string formatted with the amount, rounded based on the Currency rules, and the currency symbol

Returns a string formatted with the amount, rounded based on the Currency rules, and the currency symbol

eg USD(12.4563) => "$123.46"

Returns

String

def toFormattedString(c: Currency)(context: MoneyContext): String
override def toString: String

Returns a string formatted with the original precision amount and the currency code

Returns a string formatted with the original precision amount and the currency code

eg USD(123.456) => "123.456 USD"

Returns

String

Definition Classes
Quantity -> Any
def toString(c: Currency)(context: MoneyContext): String

Converts the amount to the given currency and returns a string formatted with the original precision and the currency code

Converts the amount to the given currency and returns a string formatted with the original precision and the currency code

Value Params
c

Currency

context

MoneyContext required for conversion

Combines with that Money to create an squants.market.CurrencyExchangeRate

Combines with that Money to create an squants.market.CurrencyExchangeRate

Exchange Rates on the same currency are not supported

val rate: CurrencyExchangeRate = JPY(100) toThe USD(1)

Value Params
that

Money

Throws
scala.IllegalArgumentException

if the that.currency matches this.currency

def value: Double

Inherited methods

def %(that: Money): Double
Inherited from
Quantity
def %(that: Double): Money
Inherited from
Quantity
def *(that: Price[Money]): Money
Inherited from
Quantity
def +(that: Money): Money
Inherited from
Quantity
def +-(that: Money): QuantityRange[Money]
Inherited from
Quantity
def -(that: Money): Money
Inherited from
Quantity
def /(that: Money): Double
Inherited from
Quantity
def /%(that: Money): (Double, Money)
Inherited from
Quantity
def /%(that: Double): (Money, Money)
Inherited from
Quantity
def <(that: Money): Boolean
Inherited from
Ordered
def <=(that: Money): Boolean
Inherited from
Ordered
def =~(that: Money)(tolerance: Money): Boolean

approx

approx

Inherited from
Quantity
def >(that: Money): Boolean
Inherited from
Ordered
def >=(that: Money): Boolean
Inherited from
Ordered
def abs: Money

Returns the absolute value of this Quantity

Returns the absolute value of this Quantity

Returns

Quantity

Inherited from
Quantity
def approx(that: Money)(tolerance: Money): Boolean

Returns boolean result of approximate equality comparison

Returns boolean result of approximate equality comparison

Value Params
that

Quantity

tolerance

Quantity

Inherited from
Quantity
def ceil: Money

Returns the smallest (closest to negative infinity) Quantity value that is greater than or equal to the argument and is equal to a mathematical integer.

Returns the smallest (closest to negative infinity) Quantity value that is greater than or equal to the argument and is equal to a mathematical integer.

Returns

Quantity

See also

java.lang.Math#ceil(double)

Inherited from
Quantity
def compareTo(that: Money): Int
Inherited from
Ordered
def divideAndRemainder(that: Money): (Double, Money)

Returns a Pair that includes the result of divideToInteger and remainder

Returns a Pair that includes the result of divideToInteger and remainder

Value Params
that

Quantity

Returns

(Double, Quantity)

Inherited from
Quantity
def divideAndRemainder(that: Double): (Money, Money)

Returns a Pair that includes the result of divideToInteger and remainder

Returns a Pair that includes the result of divideToInteger and remainder

Value Params
that

Double

Returns

(Quantity, Quantity)

Inherited from
Quantity
def floor: Money

Returns the largest (closest to positive infinity) Quantity value that is less than or equal to the argument and is equal to a mathematical integer

Returns the largest (closest to positive infinity) Quantity value that is less than or equal to the argument and is equal to a mathematical integer

Returns

Quantity

See also

java.lang.Math#floor(double)

Inherited from
Quantity

Returns an equivalent Quantity boxed with the supplied Unit

Returns an equivalent Quantity boxed with the supplied Unit

Value Params
uom

UnitOfMeasure[A]

Returns

Quantity

Inherited from
Quantity
def map(f: Double => Double): Money

Applies a function to the underlying value of the Quantity, returning a new Quantity in the same unit

Applies a function to the underlying value of the Quantity, returning a new Quantity in the same unit

Value Params
f

Double => Double function

Inherited from
Quantity

Returns the negative value of this Quantity

Returns the negative value of this Quantity

Returns

Quantity

Inherited from
Quantity
def notWithin(range: QuantityRange[Money]): Boolean

Returns true if this value is not within (contains) the range

Returns true if this value is not within (contains) the range

Value Params
range

QuantityRange

Returns

Boolean

Inherited from
Quantity

Returns a QuantityRange representing the range for this value +- that

Returns a QuantityRange representing the range for this value +- that

Value Params
that

Quantity

Returns

QuantityRange

Inherited from
Quantity
def remainder(that: Money): Double

Returns the remainder of a division by a like quantity

Returns the remainder of a division by a like quantity

Value Params
that

Quantity

Returns

Double

Inherited from
Quantity
def remainder(that: Double): Money

Returns the remainder of a division by a number

Returns the remainder of a division by a number

Value Params
that

Quantity

Returns

Quantity

Inherited from
Quantity
def rint: Money

Returns the Quantity value that is closest in value to the argument and is equal to a mathematical integer.

Returns the Quantity value that is closest in value to the argument and is equal to a mathematical integer.

Returns

Quantity

See also

java.lang.Math#rint(double)

Inherited from
Quantity
def to(uom: UnitOfMeasure[Money]): Double

Returns a Double representing the quantity in terms of the supplied unit

Returns a Double representing the quantity in terms of the supplied unit

 val d = Feet(3)
 (d to Inches) should be(36)
Value Params
uom

UnitOfMeasure[A]

Returns

Double

Inherited from
Quantity
def to(that: Money): QuantityRange[Money]

Returns a QuantityRange that goes from this to that

Returns a QuantityRange that goes from this to that

Value Params
that

Quantity

Returns

QuantityRange

Inherited from
Quantity
def toString(uom: UnitOfMeasure[Money], format: String): String

Returns a string representing the quantity's value in the given unit in the given format

Returns a string representing the quantity's value in the given unit in the given format

Value Params
format

String containing the format for the value (ie "%.3f")

uom

UnitOfMeasure[A] with UnitConverter

Returns

String

Inherited from
Quantity
def toString(uom: UnitOfMeasure[Money]): String

Returns a string representing the quantity's value in the given unit

Returns a string representing the quantity's value in the given unit

Value Params
uom

UnitOfMeasure[A] with UnitConverter

Returns

String

Inherited from
Quantity
def toTuple(uom: UnitOfMeasure[Money]): (Double, String)

Returns a pair representing the numeric value and the uom's symbol

Returns a pair representing the numeric value and the uom's symbol

Value Params
uom

UnitOfMeasure[A]

Inherited from
Quantity
def toTuple: (Double, String)

Returns a tuple representing the numeric value and the unit's symbol

Returns a tuple representing the numeric value and the unit's symbol

Inherited from
Quantity
Inherited from
Quantity
def within(range: QuantityRange[Money]): Boolean

Returns true if this value is within (contains) the range

Returns true if this value is within (contains) the range

Value Params
range

QuantityRange

Returns

Boolean

Inherited from
Quantity
def ~=(that: Money)(tolerance: Money): Boolean

approx

approx

Inherited from
Quantity
def (that: Money)(tolerance: Money): Boolean

approx

approx

Inherited from
Quantity

Concrete fields

val amount: BigDecimal