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.
See also
-
The currency represented by the
MonetaryAmount. e.g.Currency.USDDeclaration
Swift
public let currency: Currency -
The value of the
MonetaryAmountin the currency represented bycurrencyDeclaration
Swift
public let value: DynamicRoundedDecimal
-
Declaration
Swift
public var description: String { get }
-
Divides a
MonetaryAmountby aDecimalvalue.No rounding is done on the denominator prior to the calculation.
Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func / (monetaryAmount: MonetaryAmount, denominator: Decimal) -> MonetaryAmount -
Divides a
MonetaryAmountby anIntvalue.Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func / (monetaryAmount: MonetaryAmount, denominator: Int) -> MonetaryAmount -
Divides a
MonetaryAmountby anDynamicRoundedDecimalvalue.No rounding is done on the denominator prior to the calculation - since it is a
DynamicRoundedDecimalit has already been rounded.Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func / (monetaryAmount: MonetaryAmount, denominator: DynamicRoundedDecimal) -> MonetaryAmount
-
Multiplies a
MonetaryAmountby aDecimalvalue.No rounding is done on the coefficient prior to the calculation.
Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func * (monetaryAmount: MonetaryAmount, coefficient: Decimal) -> MonetaryAmount -
Multiplies a
MonetaryAmountby anIntvalue.Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func * (monetaryAmount: MonetaryAmount, coefficient: Int) -> MonetaryAmount -
Multiplies a
MonetaryAmountby anDynamicRoundedDecimalvalue.No rounding is done on the coefficient prior to the calculation - since it is a
DynamicRoundedDecimalit has already been rounded.Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func * (monetaryAmount: MonetaryAmount, coefficient: DynamicRoundedDecimal) -> MonetaryAmount -
Multiplies a
MonetaryAmountby aDecimalvalue.No rounding is done on the coefficient prior to the calculation.
Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func * (coefficient: Decimal, monetaryAmount: MonetaryAmount) -> MonetaryAmount -
Multiplies a
MonetaryAmountby anIntvalue.Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func * (coefficient: Int, monetaryAmount: MonetaryAmount) -> MonetaryAmount -
Multiplies a
MonetaryAmountby anDynamicRoundedDecimalvalue.No rounding is done on the coefficient prior to the calculation - since it is a
DynamicRoundedDecimalit has already been rounded.Rounding of the result will occur during the construction of the new
MonetaryAmountvalue with respect to itsCurrency.Declaration
Swift
public static func * (coefficient: DynamicRoundedDecimal, monetaryAmount: MonetaryAmount) -> MonetaryAmount
-
Consolidates two
MonetaryAmountvaluesThis is functionally equivalent of the
Array<MonetaryAmount>.consolidated()instance method.See the documentation for the instance method
Array<MonetaryAmount>.consolidated()for more information.Declaration
Swift
func consolidating(_ monetaryAmount: MonetaryAmount) -> [MonetaryAmount] -
Consolidates two
MonetaryAmountvaluesThis is functionally equivalent of the
Array<MonetaryAmount>.consolidated()instance method.See the documentation for the instance method
Array<MonetaryAmount>.consolidated()for more information.Declaration
Swift
func adding(_ monetaryAmount: MonetaryAmount) -> [MonetaryAmount]
-
Consolidates two
MonetaryAmountvaluesThis is functionally equivalent of the
Array<MonetaryAmount>.consolidated()instance method.See the documentation for the instance method
Array<MonetaryAmount>.consolidated()for more information.Declaration
Swift
static func | (lhs: MonetaryAmount, rhs: MonetaryAmount) -> [MonetaryAmount] -
Consolidates a
MonetaryAmountarray with anMonetaryAmountvalueThis is functionally equivalent of the
Array<MonetaryAmount>.consolidated()instance method.See the documentation for the instance method
Array<MonetaryAmount>.consolidated()for more information.Declaration
Swift
static func | (lhs: [MonetaryAmount], rhs: MonetaryAmount) -> [MonetaryAmount] -
Consolidates a
MonetaryAmountvalue with aMonetaryAmountarrayThis is functionally equivalent of the
Array<MonetaryAmount>.consolidated()instance method.See the documentation for the instance method
Array<MonetaryAmount>.consolidated()for more information.Declaration
Swift
static func | (lhs: MonetaryAmount, rhs: [MonetaryAmount]) -> [MonetaryAmount]
-
If the current value is positive, a new
MonetaryAmountis returned with the negative value. If the current value is negative, a newMonetaryAmountis 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() -> MonetaryAmountReturn Value
A new
MonetaryAmountwith an negatedvalue -
The prefix unary operator
-forMonetaryAmountis functionally equivalent to the instance methodnegated()Declaration
Swift
prefix static func - (monetaryAmount: MonetaryAmount) -> MonetaryAmount
View on GitHub
MonetaryAmount Structure Reference