Formatter for crypto token amounts.
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
f.roundingBehavior = .cutoff
f.string(from: BigDecimal(0_0000101, 7)) // 0.00001
f.roundingBehavior = .roundUp
f.string(from: BigDecimal(0_0000101, 7)) // 0.00002
f.string(from: BigDecimal(-10_000_001000000, 9) // -10,000.001
f.number(from: "0,001", precision: 3) == BigDecimal(1, 3)
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Б
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:
Drag and drop the SwiftCryptoTokenFormatter.xcodeproj
into your project and link the SwiftCryptoTokenFormatter
static library.
In your Cartfile
:
github "gnosis/SwiftCryptoTokenFormatter"
Run carthage update
to build the framework and drag the SwiftCryptoTokenFormatter.framework into your Xcode project.
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"))
]
MIT License (see the LICENSE file).
link |
Stars: 2 |
Last commit: 1 year ago |
Added check for fractional digit count to the conversion from string to number
Full Changelog: https://github.com/gnosis/SwiftCryptoTokenFormatter/compare/1.1.0...1.1.1
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics