Swifty Money is a light-weight library for handling money and currency and their formatting on iOS and macOS.
SwiftyMoney is inspired by NSMeasurement and its related classes, which are available in Apple's Foundation framework. SwiftyMoney's features are as follows:
SwiftyMoney is compatible with Swift 4.2 and newer. You can install it:
git "[email protected]:krkopec/swiftyMoney.git" "master"
to your Cartfile and running carthage update
in the Terminal.https://github.com/krkopec/swiftyMoney/
and selecting an appropriate branch or version.Before you start using SwiftyMoney, import it into your source code file by declaring: import SwiftyMoney
.
As a developer, you will use SwiftyMoney mostly through its main struct called Money. However, before you can create an instance of Money, you must first declare the currency that this instance will be using. Such a way of doing things is by design and it ensures that all necessary parameters of the currency to be used in the Money instance (and by extension the parameters of the Money itself) are properly defined before use.
In order to create an instance of Currency, you must provide:
let euro = Currency(name: "Euro",
code: "EUR",
symbol: "€",
exponent: 2)
let swedishKrona = Currency(name: "Swedish krona",
code: "SEK",
symbol: "kr",
exponent: 2)
In order to ease the management of different currencies, it is advisable to extend the Currency class by using static properties, as follows:
// Extend Currency class
extension Currency {
public static let polishZloty = Currency(name: "Polish zloty",
code: "PLN",
symbol: "zł",
exponent: 2)
}
In this way, you will be able to use a currency in a manner similar to an enumeration, as presented below:
let defaultCurrency = Currency.polishZloty
As mentioned above, in order to create an instance of Money, you must first declare a currency to be used. Please note that the examples below use currencies declared as static properties of the Currency class, as described above. You can declare Money, as follows.
// Create a money amount
let tenEuro = Money(value: 10, currency: .euro)
let twentyKrona = Money(value: 20, currency: .swedishKrona)
Money conforms to the Equatable and Comparable protocols, so that it is possible to compare its instances.
Money(value: 10, currency: .euro) == Money(value: 10, currency: .euro) // == true
Money(value: 10, currency: .euro) != Money(value: 10, currency: .euro) // == false
Money(value: 10, currency: .euro) < Money(value: 10, currency: .euro) // == false
You can compare monies provided in different currencies. This, however, requires that you set Money's default currency converter and its baseCurrency, as shown below:
Money.setCurrencyConverter(to: CurrencyConverter(baseCurrency: .euro,
currencyExchangeRates: [euroToUSDollarConversionRate]))
Afterwards, you can compare amounts in two different currencies.
Money(value: 10, currency: .euro) == Money(value: 10, currency: .usDollar)
Money(value: 10, currency: .euro) != Money(value: 10, currency: .usDollar)
Money(value: 10, currency: .euro) < Money(value: 10, currency: .usDollar)
Basic arithmetic examples:
Money(value: 10, currency: .euro) + Money(value: 10, currency: .euro) // == 20 euro
Money(value: 10, currency: .euro) - Money(value: 5, currency: .euro) // == 5 euro
Money(value: 10, currency: .euro) * 2 // == 20 euro
Money(value: 10, currency: .euro) / 2 // == 5 euro
As with comparisons, you can perform mathematical transaction on monies of the same or different currencies. Depending on the circumstance, the result of addition or subtraction of any money amounts may be returned:
Money(value: 10, currency: .euro) + Money(value: 10, currency: .euro) // == 20 euro
// Money.currencyConverter.baseCurrency == .euro and eurToUsdConversionRate == 1.12979
Money(value: 10, currency: .euro) + Money(value: 10, currency: .usDollar) // == 18.85 euro
// Money.currencyConverter.baseCurrency == nil
Money(value: 10, currency: .euro) + Money(value: 10, currency: .usDollar) // == nil
In order to convert monies to other currencies, you must declare CurrencyConverter first. This may be done through Money's currencyConverter static property or as a separate instance of CurrencyConverter.
CurrencyConverter converts currencies:
In order to create a currency converter, you need specify
// set converter to convert from Euro to US Dollar.
let converter = CurrencyConverter(currencyExchangeRates: [euroToUSDollarRate])
// set converter to convert between a number of currencies through common Euro conversion rate.
let converter = CurrencyConverter(baseCurrency: .euro,
currencyExchangeRates: [euroToUSDollarRate,
euroToPolishZloty,
euroToSwedishKronaRate])
In order to use currency converter you must first declare currency exchange rates, as follows:
let euroToKronaRate = CurrencyExchangeRate(sourceCurrency: .euro,
targetCurrency: .swedishKrona,
allowsInverseConversion: true,
sourceToTargetRate: 10.7405)
// if the allowsInverseConversion property true, then targetToSourceRate == 1 / 10.7405
As suggested in the comment above, thanks to the allowsInverseConversion property, CurrencyExchangeRate allows for converting money in one direction (e.g. from EUR to USD) or in two directions (e.g. from EUR to USD and USD to EUR) using one currency exchange rate.
link |
Stars: 0 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics