MonetaryAmount

public struct MonetaryAmount : Equatable, Hashable, CustomStringConvertible

A Swift representation of money / monetary amounts and ISO 4217 currency designations. Supports manipulation and combination of monetary amounts of a single currency or multiple currencies.

Handles minor units (the exponent, e.g. cents for USD) for each currency as specified in the ISO 4217 standard. Simple manipulation includes consolidating / combining amounts.

MonetaryAmount values that have the same Currency will be added together, MonetaryAmount values with unique Currency values will be added to the result on their own.

Usage Example

let moneyA = 12.00.in(.USD)
let moneyB = 18.00.in(.USD)
let moneyC = 6.00.in(.GBP)

// result would equal [30.00.in(.USD), 6.00.in(.GBP)]
let result = [moneyA, moneyB, moneyC].consolidating(moneyB)

The underlying value for the amount is represented by a DynamicRoundedDecimal which itself uses the Swift Decimal type. DynamicRoundedDecimal handles rounding internally as declared by the given currency’s minorUnit.

Usage Example

// moneyA and moneyB are equal and represent US$28.53
let moneyA = MonetaryAmount(currency: Currency.USD, value: Decimal(string: "28.529372")!)
let moneyB = 28.53.in(.USD)

Note

MonetaryAmount does not do any FX or conversion of currencies, it keeps each individual currency subtotal separate.

  • The currency represented by the MonetaryAmount. e.g. Currency.USD

    Declaration

    Swift

    public let currency: Currency
  • The value of the MonetaryAmount in the currency represented by currency

    Declaration

    Swift

    public let value: DynamicRoundedDecimal
  • Declaration

    Swift

    public var description: String { get }
  • Divides a MonetaryAmount by a Decimal value.

    No rounding is done on the denominator prior to the calculation.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func / (monetaryAmount: MonetaryAmount, denominator: Decimal) -> MonetaryAmount
  • Divides a MonetaryAmount by an Int value.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func / (monetaryAmount: MonetaryAmount, denominator: Int) -> MonetaryAmount
  • Divides a MonetaryAmount by an DynamicRoundedDecimal value.

    No rounding is done on the denominator prior to the calculation - since it is a DynamicRoundedDecimal it has already been rounded.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func / (monetaryAmount: MonetaryAmount, denominator: DynamicRoundedDecimal) -> MonetaryAmount
  • Multiplies a MonetaryAmount by a Decimal value.

    No rounding is done on the coefficient prior to the calculation.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func * (monetaryAmount: MonetaryAmount, coefficient: Decimal) -> MonetaryAmount
  • Multiplies a MonetaryAmount by an Int value.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func * (monetaryAmount: MonetaryAmount, coefficient: Int) -> MonetaryAmount
  • Multiplies a MonetaryAmount by an DynamicRoundedDecimal value.

    No rounding is done on the coefficient prior to the calculation - since it is a DynamicRoundedDecimal it has already been rounded.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func * (monetaryAmount: MonetaryAmount, coefficient: DynamicRoundedDecimal) -> MonetaryAmount
  • Multiplies a MonetaryAmount by a Decimal value.

    No rounding is done on the coefficient prior to the calculation.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func * (coefficient: Decimal, monetaryAmount: MonetaryAmount) -> MonetaryAmount
  • Multiplies a MonetaryAmount by an Int value.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func * (coefficient: Int, monetaryAmount: MonetaryAmount) -> MonetaryAmount
  • Multiplies a MonetaryAmount by an DynamicRoundedDecimal value.

    No rounding is done on the coefficient prior to the calculation - since it is a DynamicRoundedDecimal it has already been rounded.

    Rounding of the result will occur during the construction of the new MonetaryAmount value with respect to its Currency.

    Declaration

    Swift

    public static func * (coefficient: DynamicRoundedDecimal, monetaryAmount: MonetaryAmount) -> MonetaryAmount
  • If the current value is positive, a new MonetaryAmount is returned with the negative value. If the current value is negative, a new MonetaryAmount is returned with a positive value.

    Example:

    // money is a MonetaryAmount of $12.98
    let money = 12.98.in(.USD)
    
    // negativeAmount is a MonetaryAmount of -$12.98
    let negativeAmount = money.negated()
    
    // positiveAmount is a MonetaryAmount of $12.98
    let positiveAmount = negativeAmount.negated()
    

    Declaration

    Swift

    func negated() -> MonetaryAmount

    Return Value

    A new MonetaryAmount with an negated value

  • The prefix unary operator - for MonetaryAmount is functionally equivalent to the instance method negated()

    Declaration

    Swift

    prefix static func - (monetaryAmount: MonetaryAmount) -> MonetaryAmount