Swiftpack.co -  gnosis/SwiftCryptoTokenFormatter as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
gnosis/SwiftCryptoTokenFormatter
Formatter for crypto token amounts
.package(url: "https://github.com/gnosis/SwiftCryptoTokenFormatter.git", from: "1.1.0")

SwiftCryptoTokenFormatter

Formatter for crypto token amounts.

Rules

  • Cut off after the 5th decimal, no matter how many decimals there are: 0.12345
  • Remove trailing zeroes, i.e. display 0.10000 as 0.1
  • Use the 5 decimals up until 999.99999
  • Display '< 0.00001' for values less than 0.00001
  • Display '< -0.00001' for values greater than -0.00001 and less than 0 despite the fact that it's semantically not 100% correct as e.g. -0.0000005 is bigger, not smaller than -0.00001
  • Use 1 decimal less from 1,000.0001 until 9,999.9999
  • Use 1 decimal less from 10,000.001 until 99,999.999
  • Use 1 decimal less from 100,000.01 until 999,999.99
  • Use 1 decimal less from 1,000,000.1 until 9,999,999.9
  • From 10,000,000 No Decimals until 99,999,999
  • Then Use 10.001M until 999.999M
  • Then 1.001B until 999.999B
  • Then 1.001T until 999.999T
  • Then just > 999T
  • Thousands and decimal separators are used according to user's locale.
  • M, B, T is localized
import SwiftCryptoTokenFormatter
import BigInt

let f = TokenFormatter()
f.string(from: BigDecimal(100_000_000_000000000, 9)) // 100M
f.string(from: BigDecimal(BigInt("999999999000000000000"), 9)) // 999.999B

Features

Custom rounding behaviour

f.roundingBehavior = .cutoff
f.string(from: BigDecimal(0_0000101, 7)) // 0.00001
f.roundingBehavior = .roundUp
f.string(from: BigDecimal(0_0000101, 7)) // 0.00002

Support of negative numbers

f.string(from: BigDecimal(-10_000_001000000, 9) // -10,000.001

Number from string

f.number(from: "0,001", precision: 3) == BigDecimal(1, 3)

Localizatoin

string(from:) allows to override default decimalSeparator, thousandSeparator and literals.

localizedString(from:) uses:

  • Locale.autoupdatingCurrent for decimalSeparator and thousandSeparator
  • "amount_millions", "amount_billions" and "amount_trillions" localized strings from literals. You need to provide localizable strings for different locales.
// Assuming russian locale and "amount_billions" = "Б"
formatter.localizedString(from: BigDecimal(BigInt("999999999000000000000"), 9)) // 999,999Б

Installation

Prerequisites

  • iOS 11.0 or macOS 10.14
  • Xcode 10.3
  • Swift 5

Manual

Add this repository as a submodule:

git submodule add https://github.com/gnosis/SwiftCryptoTokenFormatter.git

Fetch the dependencies

cd SwiftCryptoTokenFormatter
git submodule update --init

Dependencies of the SwiftCryptoTokenFormatter library:

  • BigInt

Drag and drop the SwiftCryptoTokenFormatter.xcodeproj into your project and link the SwiftCryptoTokenFormatter static library.

Carthage

In your Cartfile: github "gnosis/SwiftCryptoTokenFormatter"

Run carthage update to build the framework and drag the SwiftCryptoTokenFormatter.framework into your Xcode project.

Swift Package Manager

You can use Swift Package Manager and add dependency in your Package.swift:

    dependencies: [
        .package(url: "https://github.com/gnosis/SwiftCryptoTokenFormatter.git", .upToNextMinor(from: "1.1.0"))
    ]

Contributors

License

MIT License (see the LICENSE file).

GitHub

link
Stars: 1
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

Minor update
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API