Swiftpack.co - Package - peek-travel/swift-currency

Swift Currency

MIT License Swift 5+

Package Tests codecov

Documentation Maintainability


Swift Currency provides type-safe representations of ISO 4217 currencies in Swift.

It provides many conveniences for working with currencies, such as literal representations, string interpolation, and mathematics.

import Currency
import Foundation

let dollars = USD(30.01)
// 30.01 USD
print(dollars * 2)
// 60.02 USD
print(dollars.distributedEvenly(intoParts: 6))
// [USD(1.68), USD(1.68), USD(1.68), USD(1.67), USD(1.67), USD(1.67)]

let pounds = GBP(109.23)
print(dollars + pounds)
// compile error

let jpy: JPY = 399
print("The total price is \(jpy.localizedString()).")
// "The total price is ¥399.", assuming `Foundation.Locale.current` is "en_US"

let euro = EUR(29.09)
print("Der Gesamtpreis beträgt \(localize: euro, for: .init(identifier: "de_DE")).")
// "Der Gesamtpreis beträgt 29,09 €."

For more detailed examples, see the usage guide.

Language and Platform Test Matrix

The following table shows the combination of Swift language versions and operating systems that receive regular unit testing.

| Platform | Swift 5 | 5.1 | 5.2 | Trunk | |:---------|:-------:|:---:|:---:|:-----:| | macOS Latest | ✅ | ✅ | ✅ | | | Ubuntu 18.04 (Bionic) | ✅ | ✅ | ✅ | ✅ | | Ubuntu 16.04 (Xenial) | ✅ | ✅ | ✅ | ✅ |


Swift Package Manager

The preferred installation method is with SPM. Just add the package reference to your Package.swift.

dependencies: [
    .package(url: "https://github.com/peek-travel/swift-currency", from: "0.6.0")


To install with Carthage, just add the line gh "https://github.com/peek-travel/swift-currency" ~> 0.6.0 to your Cartfile.


The API docs for the latest tagged release are always available at peek-travel.github.io/swift-currency.

For "how to use" guides, and additional information about the project, see the documentation.


For bugs or feature requests, file a new issue.


SemVer changes are documented for each release on the releases page.


Check out CONTRIBUTING.md for more information on how to help with SwiftCurrency.


Check out CONTRIBUTORS.txt to see the full list. This list is updated for each release.



Copyright (c) 2020-present, Peek Travel Inc.

This project contains code written by others not affliated with this project. All copyright claims are reserved by them. For a full list, with their claimed rights, see NOTICE.txt

Swift is a registered trademark of Apple, Inc. Any use of their trademark does not imply any affiliation with or endorsement by them, and all rights are reserved by them.


Stars: 65


Used By

Total: 0


SwiftCurrency 0.6.0 - 2020-03-08 00:58:12

API Docs are always available at peek-travel.github.io/swift-currency


  • Currency localization is now easier to use with localizedString methods on AnyCurrency that reflect the String interpolation API (#34)
    • The String interpolation methods (localize:forLocale:nilDescription:) and (localize:withFormatter:nilDescription:) have had their 2nd parameters renamed to for: and with: respectively.


  • A usage doc is now available that gives an overview of many aspects of the library (#35)

Currency 0.5.0 - 2020-02-29 05:03:40

API Docs are always available at peek-travel.github.io/swift-currency


  • CurrencyMint has been refactored to be more flexible for 3rd party currencies (#21)

Currency 0.4.1 - 2020-02-29 03:42:16

API Docs are always available at peek-travel.github.io/swift-currency


  • Add CustomLeafReflectable conformance to AnyCurrency (#31)
  • The XTS (Test) currency has been added (#32)


  • The performance of * and / operations on currencies has been improved by 50% (#30)
  • Change XXX (No currency transaction) currency minor units from 2 to 0 (#33)
    • This is considered a bugfix, as ISO 4217 does not specify any "exponent" (minorUnits) value for XXX, so we are arbitrarily choosing 0.

Currency 0.4.0 - 2020-02-23 22:33:58

API Docs are always available at peek-travel.github.io/swift-currency


  • The protocol hierarchy implementation of AnyCurrency and CurrencyProtocol has been reimplemented to resolve incorrect Swift semantics (#25)
    • The most notable breaking changes are to initializers:
      • init(minorUnits) is now generic on BinaryInteger
      • init(_:) is now failable and requires the amount: label
    • The inverseAmount computed property is now a method named negated()
    • Arithmetic is now only possible between instances of currencies
  • Localization through String Interpolation now requires the localize: prefix (#25)
    • e.g. "\(localize: JPY(300))"


  • The XXX (No Currency Transaction) currency has been added (#20)


  • Fixed #17 by making the init(_:) failable to handle .nan values (#25)


  • API documentation is now available for each latest release (#26)
  • The project now has several maturity benchmarks
    • Code coverage (#19)
    • Linux unit testing (#24)
    • macOS unit testing for Swift 5.2 (#27)
    • Code Climate ratings (#28)

Currency 0.3.1 - 2020-01-22 04:18:15


  • CurrencyMint now has methods for looking up a Currency Type, rather than just an instance.

Currency 0.3.0 - 2020-01-21 06:14:19


  • AnyCurrency storage is now "minorUnits" https://github.com/peek-travel/swift-currency/commit/44425d8fee78044797419525cf25b19860e7b7d8
    • AnyCurrency now uses Int64 "minorUnits" as the underlying storage instead of Foundation.Decimal "exactAmount"
    • AnyCurrency.roundedAmount is now amount
    • AnyCurrency.init(minorUnits:) is now .init(exactly:)
      • This is now required by the AnyCurrency protocol
  • _CurrencyImplementation has been renamed and made public as CurrencyProtocol https://github.com/peek-travel/swift-currency/commit/d4fcba861b697b3064c2b45c72557221f6e71a62
    • This is the protocol to reference as a generic constraint, while AnyCurrency can be used as a type-erased existential


  • AnyCurrency now has a "minorUnits" property https://github.com/peek-travel/swift-currency/commit/44425d8fee78044797419525cf25b19860e7b7d8
  • AnyCurrency now has two methods for distribution algorithms https://github.com/peek-travel/swift-currency/commit/46f75f09a2442efd4e050932e199a9cee312d5f8
    • distributedEvenly(intoParts:)
    • distributedProportionally(between:)
  • AnyCurrency now has an inverse computed property for getting the opposite signed value https://github.com/peek-travel/swift-currency/commit/46f75f09a2442efd4e050932e199a9cee312d5f8
  • Sequences with AnyCurrency elements now has a inlined mapping sum(_:) overload https://github.com/peek-travel/swift-currency/commit/b898bb436aaee1c8bf27043427f9205fd3408f31


  • The Currency module should now be able to be embedded in iOS apps https://github.com/peek-travel/swift-currency/commit/46aa110c31394dc26d229f32deb56670150f4c2b
  • sum and sum(where:) should now property be O(n) and inlinable for more performance https://github.com/peek-travel/swift-currency/commit/71285fe730e195e2d8315b6928a8bfeed80c6f54


  • Tests are now run against Swift 5 in addition to 5.1

Currency 0.2.0 - 2020-01-15 22:06:13


  • Money was renamed to AnyCurrency
    • The currency property was renamed to metadata


  • AnyCurrency now has a static property named metadata as well
    • Any type that conforms to both AnyCurrency and CurrencyMetadata will have this requirement met by default
  • Sequence types with AnyCurrency elements now have a sum() and sum(where:) method for aggregating values


  • Carthage is now supported as a dependency manager

Currency 0.1.1: Carthage Support - 2020-01-15 00:43:11

- 2020-01-13 18:03:37