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.USD
Declaration
Swift
public let currency: Currency
-
The value of the
MonetaryAmount
in the currency represented bycurrency
Declaration
Swift
public let value: DynamicRoundedDecimal
-
Declaration
Swift
public var description: String { get }
-
Divides a
MonetaryAmount
by aDecimal
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 itsCurrency
.Declaration
Swift
public static func / (monetaryAmount: MonetaryAmount, denominator: Decimal) -> MonetaryAmount
-
Divides a
MonetaryAmount
by anInt
value.Rounding of the result will occur during the construction of the new
MonetaryAmount
value with respect to itsCurrency
.Declaration
Swift
public static func / (monetaryAmount: MonetaryAmount, denominator: Int) -> MonetaryAmount
-
Divides a
MonetaryAmount
by anDynamicRoundedDecimal
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 itsCurrency
.Declaration
Swift
public static func / (monetaryAmount: MonetaryAmount, denominator: DynamicRoundedDecimal) -> MonetaryAmount
-
Multiplies a
MonetaryAmount
by aDecimal
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 itsCurrency
.Declaration
Swift
public static func * (monetaryAmount: MonetaryAmount, coefficient: Decimal) -> MonetaryAmount
-
Multiplies a
MonetaryAmount
by anInt
value.Rounding of the result will occur during the construction of the new
MonetaryAmount
value with respect to itsCurrency
.Declaration
Swift
public static func * (monetaryAmount: MonetaryAmount, coefficient: Int) -> MonetaryAmount
-
Multiplies a
MonetaryAmount
by anDynamicRoundedDecimal
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 itsCurrency
.Declaration
Swift
public static func * (monetaryAmount: MonetaryAmount, coefficient: DynamicRoundedDecimal) -> MonetaryAmount
-
Multiplies a
MonetaryAmount
by aDecimal
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 itsCurrency
.Declaration
Swift
public static func * (coefficient: Decimal, monetaryAmount: MonetaryAmount) -> MonetaryAmount
-
Multiplies a
MonetaryAmount
by anInt
value.Rounding of the result will occur during the construction of the new
MonetaryAmount
value with respect to itsCurrency
.Declaration
Swift
public static func * (coefficient: Int, monetaryAmount: MonetaryAmount) -> MonetaryAmount
-
Multiplies a
MonetaryAmount
by anDynamicRoundedDecimal
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 itsCurrency
.Declaration
Swift
public static func * (coefficient: DynamicRoundedDecimal, monetaryAmount: MonetaryAmount) -> MonetaryAmount
-
Consolidates two
MonetaryAmount
valuesThis 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
MonetaryAmount
valuesThis 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
MonetaryAmount
valuesThis 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
MonetaryAmount
array with anMonetaryAmount
valueThis 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
MonetaryAmount
value with aMonetaryAmount
arrayThis 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
MonetaryAmount
is returned with the negative value. If the current value is negative, a newMonetaryAmount
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 negatedvalue
-
The prefix unary operator
-
forMonetaryAmount
is functionally equivalent to the instance methodnegated()
Declaration
Swift
prefix static func - (monetaryAmount: MonetaryAmount) -> MonetaryAmount