Packages

  • package root
    Definition Classes
    root
  • package squants

    The Scala API for Quantities, Units of Measure and Dimensional Analysis

    Squants

    The Scala API for Quantities, Units of Measure and Dimensional Analysis

    Overview

    Squants is a framework of data types and a domain specific language (DSL) for representing Quantities, their Units of Measure, and their Dimensional relationships. The API supports typesafe dimensional analysis, improved domain models and more. All types are immutable and thread-safe.

    Typedefs and implicits for common usages

    Definition Classes
    root
    Version

    0.1

    Since

    0.1

  • package market

    Squants Market API

    Squants Market API

    Market Types are similar but not quite the same as other quantities in the library.

    The primary type, squants.market.Money, is derived from Quantity, and its Units of Measure are Currencies. However, because the conversion multipliers between units can not be predefined, many of the behaviors have been overridden and augmented to realize correct behavior.

    squants.market.Prices represent a Ratio between Money and some other Quantity. Prices can be created from ratios of Money and a Quantity

    val money: Money = USD(10)
    val length: Length = Meters(1)
    val price: Price[Length] = money / length

    Multiplying a Price * Quantity will yield Money amount that represents the cost of the quantity

    val cost: Money = price * Meters(3.8)

    and multiplying Price * Money will yield the corresponding Quantity amount

    val budget: Money = USD(250)
    val quote: Length = price * budget

    squants.market.CurrencyExchangeRates represent conversion rates between currencies. Use them to explicitly convert Money values in one currency to values in another.

    squants.market.MoneyContext provide the implicit context necessary to perform cross-currency operations on Money values with conversions automatically applied.

    Some binary math operations will work on Moneys of like Currency with no MoneyContext in scope. Attempts to perform these operations on Moneys of dissimilar currencies will throw an exception at runtime.

    Other operations, including direct conversions to other currencies, require a MoneyContext and will not compile without it. However, there is no compile time check to determine if the correct exchange rates will be available at runtime. Operation requiring conversion without the required rates available will throw a NoSuchExchangeRateException at runtime.

    The defaultMoneyContext uses the USD as the default and provides a list of ~20 common currencies, and NO exchange rates. If your application requires something different you should initialize your own implicit MoneyContext

    Definition Classes
    squants
    Since

    0.1

  • ARS
  • AUD
  • BRL
  • BTC
  • CAD
  • CHF
  • CLP
  • CNY
  • CZK
  • Currency
  • CurrencyExchangeRate
  • DKK
  • ETH
  • EUR
  • GBP
  • HKD
  • INR
  • JPY
  • KRW
  • LTC
  • MXN
  • MYR
  • Money
  • MoneyContext
  • MoneyConversions
  • NAD
  • NOK
  • NZD
  • NoSuchCurrencyException
  • NoSuchExchangeRateException
  • Price
  • RUB
  • SEK
  • USD
  • XAG
  • XAU
  • ZAR

final class Money extends Quantity[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

Source
Money.scala
Since

0.1

Linear Supertypes
Quantity[Money], Ordered[Money], Comparable[Money], Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Money
  2. Quantity
  3. Ordered
  4. Comparable
  5. Serializable
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. def !=#(that: Money)(implicit moneyContext: MoneyContext): Boolean
  3. final def ##(): Int
    Definition Classes
    AnyRef → Any
  4. 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

    that

    BigDecimal

    returns

    Money

  5. def %(that: Money): Double
    Definition Classes
    Quantity
  6. def %(that: Double): Money
    Definition Classes
    Quantity
  7. def *(that: CurrencyExchangeRate): Money

    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

    that

    BigDecimal

  8. def *(that: Double): Money
    Definition Classes
    MoneyQuantity
  9. def *(that: BigDecimal): Money
  10. def *(that: squants.Price[Money]): squants.Money
    Definition Classes
    Quantity
  11. def +(that: Money)(implicit context: MoneyContext = defaultMoneyContext): Money

    moneyPlus *

  12. def +(that: Money): Money
    Definition Classes
    Quantity
  13. def +-(that: Money): QuantityRange[Money]
    Definition Classes
    Quantity
  14. def -(that: Money)(implicit context: MoneyContext = defaultMoneyContext): Money

    moneyMinus *

  15. def -(that: Money): Money
    Definition Classes
    Quantity
  16. def ->: (Money) => CurrencyExchangeRate

    toThe

  17. def /[A <: Quantity[A]](that: squants.Price[A]): A

    Divide this money by a Price and return Quantity

    Divide this money by a Price and return Quantity

    A

    Quantity Type

    that

    Price

    returns

    A

  18. def /[A <: Quantity[A]](that: A): squants.Price[A]

    Divide this money by another (non-money) Quantity and return a Price

    Divide this money by another (non-money) Quantity and return a Price

    A

    Quantity Type

    that

    Quantity

    returns

    Price[A]

  19. def /(that: Money)(implicit context: MoneyContext = defaultMoneyContext): BigDecimal
  20. def /(that: Double): Money
    Definition Classes
    MoneyQuantity
  21. def /(that: BigDecimal): Money
  22. def /(that: Money): Double
    Definition Classes
    Quantity
  23. 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

    that

    BigDecimal

    returns

    (Money, Money)

  24. def /%(that: Money): (Double, Money)
    Definition Classes
    Quantity
  25. def /%(that: Double): (Money, Money)
    Definition Classes
    Quantity
  26. def <(that: Money): Boolean
    Definition Classes
    Ordered
  27. def <#(that: Money)(implicit moneyContext: MoneyContext): Boolean
  28. def <=(that: Money): Boolean
    Definition Classes
    Ordered
  29. def <=#(that: Money)(implicit moneyContext: MoneyContext): Boolean
  30. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  31. def ==#(that: Money)(implicit moneyContext: MoneyContext): Boolean
  32. def =~(that: Money)(implicit tolerance: Money): Boolean

    approx

    approx

    Definition Classes
    Quantity
  33. def >(that: Money): Boolean
    Definition Classes
    Ordered
  34. def >#(that: Money)(implicit moneyContext: MoneyContext): Boolean
  35. def >=(that: Money): Boolean
    Definition Classes
    Ordered
  36. def >=#(that: Money)(implicit moneyContext: MoneyContext): Boolean
  37. def abs: Money

    Returns the absolute value of this Quantity

    Returns the absolute value of this Quantity

    returns

    Quantity

    Definition Classes
    Quantity
  38. val amount: BigDecimal
  39. def approx(that: Money)(implicit tolerance: Money): Boolean

    Returns boolean result of approximate equality comparison

    Returns boolean result of approximate equality comparison

    that

    Quantity

    tolerance

    Quantity

    Definition Classes
    Quantity
  40. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  41. 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

    Definition Classes
    Quantity
    See also

    java.lang.Math#ceil(double)

  42. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  43. 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

    that

    Money

    returns

    Int

    Definition Classes
    MoneyQuantity → Ordered
  44. def compareTo(that: Money): Int
    Definition Classes
    Ordered → Comparable
  45. val currency: Currency
  46. def dimension: Money

    The Dimension this quantity represents

    The Dimension this quantity represents

    Definition Classes
    MoneyQuantity
  47. 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

    that

    Money

    returns

    Double

    Definition Classes
    MoneyQuantity
  48. def divide(that: Double): Money

    Overrides Quantity.divide to ensure BigDecimal math is performed

    Overrides Quantity.divide to ensure BigDecimal math is performed

    that

    Double

    returns

    Quantity

    Definition Classes
    MoneyQuantity
  49. 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

    that

    BigDecimal

    returns

    Money

  50. 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

    that

    Quantity

    returns

    (Double, Quantity)

    Definition Classes
    Quantity
  51. 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

    that

    Double

    returns

    (Quantity, Quantity)

    Definition Classes
    Quantity
  52. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  53. 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

    that

    Money must be of matching value and unit

    Definition Classes
    MoneyQuantity → AnyRef → Any
  54. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  55. 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

    Definition Classes
    Quantity
    See also

    java.lang.Math#floor(double)

  56. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  57. 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
    MoneyQuantity → AnyRef → Any
  58. def in(unit: Currency)(implicit 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

    unit

    Currency

    context

    MoneyContext required for cross currency operations

    returns

    Money

    Exceptions thrown

    NoSuchExchangeRateException when no exchange rate is available

  59. def in(uom: UnitOfMeasure[Money]): Money

    Returns an equivalent Quantity boxed with the supplied Unit

    Returns an equivalent Quantity boxed with the supplied Unit

    uom

    UnitOfMeasure[A]

    returns

    Quantity

    Definition Classes
    Quantity
  60. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  61. 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

    f

    Double => Double function

    Definition Classes
    Quantity
  62. 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

    f

    BigDecimal => BigDecimal function

    returns

    Money

  63. 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

    that

    Money

    returns

    Int

    Definition Classes
    MoneyQuantity
  64. 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

    that

    Quantity

    returns

    Int

    Definition Classes
    MoneyQuantity
  65. 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

    that

    Money

    returns

    Money

    Definition Classes
    MoneyQuantity
    Exceptions thrown

    scala.UnsupportedOperationException when attempted on cross currencies

  66. def moneyCompare(that: Money)(implicit moneyContext: MoneyContext): Int

    Supports compare operation on Moneys of dislike Currency

    Supports compare operation on Moneys of dislike Currency

    that

    Money

    moneyContext

    MoneyContext

  67. def moneyDivide(that: Money)(implicit 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

    that

    Money

    context

    MoneyContext

  68. def moneyEquals(that: Money)(implicit moneyContext: MoneyContext): Boolean

    Supports equality comparisons on Moneys of dislike Currency

    Supports equality comparisons on Moneys of dislike Currency

    that

    Money

    moneyContext

    MoneyContext

  69. def moneyMax(that: Money)(implicit moneyContext: MoneyContext): Money

    Supports max operation on Moneys of dislike Currency

    Supports max operation on Moneys of dislike Currency

    that

    Money

    moneyContext

    MoneyContext

  70. def moneyMin(that: Money)(implicit moneyContext: MoneyContext): Money

    Supports min operation on Moneys of dislike Currency

    Supports min operation on Moneys of dislike Currency

    that

    Money

    moneyContext

    MoneyContext

  71. def moneyMinus(that: Money)(implicit context: MoneyContext = defaultMoneyContext): 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

    that

    Money

    context

    MoneyContext required for cross currency operations

    returns

    Money

    Exceptions thrown

    NoSuchExchangeRateException when no exchange rate is available

  72. def moneyNotEquals(that: Money)(implicit moneyContext: MoneyContext): Boolean

    Supports non-equality comparisons on Moneys of dislike Currency

    Supports non-equality comparisons on Moneys of dislike Currency

    that

    Money

    moneyContext

    MoneyContext

  73. def moneyPlus(that: Money)(implicit context: MoneyContext = defaultMoneyContext): 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

    that

    Money

    context

    MoneyContext required for cross currency operations

    returns

    Money

    Exceptions thrown

    NoSuchExchangeRateException when no exchange rate is available

  74. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  75. def negate: Money

    Returns the negative value of this Quantity

    Returns the negative value of this Quantity

    returns

    Quantity

    Definition Classes
    Quantity
  76. 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

    range

    QuantityRange

    returns

    Boolean

    Definition Classes
    Quantity
  77. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  78. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  79. def plus(that: Money): Money

    Overrides Quantity.plus to only work on like currencies.

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

    that

    Money

    returns

    Money

    Definition Classes
    MoneyQuantity
    Exceptions thrown

    scala.UnsupportedOperationException when attempted on cross currencies

  80. def plusOrMinus(that: Money): QuantityRange[Money]

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

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

    that

    Quantity

    returns

    QuantityRange

    Definition Classes
    Quantity
  81. 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

    that

    Quantity

    returns

    Double

    Definition Classes
    Quantity
  82. def remainder(that: Double): Money

    Returns the remainder of a division by a number

    Returns the remainder of a division by a number

    that

    Quantity

    returns

    Quantity

    Definition Classes
    Quantity
  83. 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

    Definition Classes
    Quantity
    See also

    java.lang.Math#rint(double)

  84. def rounded(scale: Int, mode: RoundingMode = RoundingMode.HALF_EVEN): Money

    Returns a Money rounded using scale and mode.

    Returns a Money rounded using scale and mode.

    scale

    Int - scale of the Money to be returned

    mode

    RoundingMode - defaults to HALF_EVEN

    returns

    Quantity

    Definition Classes
    MoneyQuantity
  85. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  86. def times(that: Double): Money

    Overrides Quantity.times to ensure BigDecimal math is performed

    Overrides Quantity.times to ensure BigDecimal math is performed

    that

    Double

    returns

    Quantity

    Definition Classes
    MoneyQuantity
  87. 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

    that

    BigDecimal

    returns

    Money

  88. def to(unit: Currency)(implicit context: MoneyContext): BigDecimal

    Convert this Money to a Double representing the currency unit

    Convert this Money to a Double representing the currency unit

    unit

    Currency

    context

    MoneyContext required for cross currency operations

    returns

    Double

    Exceptions thrown

    NoSuchExchangeRateException when no exchange rate is available

  89. 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)
    uom

    UnitOfMeasure[A]

    returns

    Double

    Definition Classes
    Quantity
  90. def to(that: Money): QuantityRange[Money]

    Returns a QuantityRange that goes from this to that

    Returns a QuantityRange that goes from this to that

    that

    Quantity

    returns

    QuantityRange

    Definition Classes
    Quantity
  91. def toFormattedString(c: Currency)(implicit context: MoneyContext): String
  92. 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

  93. def toString(c: Currency)(implicit 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

    c

    Currency

    context

    MoneyContext required for conversion

  94. 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
    MoneyQuantity → AnyRef → Any
  95. 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

    uom

    UnitOfMeasure[A] with UnitConverter

    format

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

    returns

    String

    Definition Classes
    Quantity
  96. 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

    uom

    UnitOfMeasure[A] with UnitConverter

    returns

    String

    Definition Classes
    Quantity
  97. def toThe(that: Money): CurrencyExchangeRate

    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)

    that

    Money

    Exceptions thrown

    scala.IllegalArgumentException if the that.currency matches this.currency

  98. 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

    uom

    UnitOfMeasure[A]

    Definition Classes
    Quantity
  99. 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

    Definition Classes
    Quantity
  100. def unary_-(): Money
    Definition Classes
    Quantity
  101. def unit: Currency

    The Unit of Measure the value represents

    The Unit of Measure the value represents

    returns

    UnitOfMeasure[A]

    Definition Classes
    MoneyQuantity
  102. def value: Double

    The value of the quantity given the unit

    The value of the quantity given the unit

    returns

    Double

    Definition Classes
    MoneyQuantity
  103. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  104. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  105. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  106. 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

    range

    QuantityRange

    returns

    Boolean

    Definition Classes
    Quantity
  107. def ~=(that: Money)(implicit tolerance: Money): Boolean

    approx

    approx

    Definition Classes
    Quantity
  108. def (that: Money)(implicit tolerance: Money): Boolean

    approx

    approx

    Definition Classes
    Quantity

Inherited from Quantity[Money]

Inherited from Ordered[Money]

Inherited from Comparable[Money]

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped