SwiftCryptoTokenFormatter 1.0.0

SwiftCryptoTokenFormatter 1.0.0

Andrey Scherbovich 维护。



  • Dmitry Bespalov 和 Andrey Scherbovich

SwiftCryptoTokenFormatter

加密代币金额格式化工具。

规则

  • 保留到小数点后第五位,无论小数点后有几位:0.12345
  • 移除末尾的零,例如显示 0.10000 为 0.1
  • 保留 5 位小数直到 999.99999
  • 对于小于 0.00001 的值显示 '< 0.00001'
  • 从 1,000.0001 到 9,999.9999 使用 1 位小数
  • 从 10,000.001 到 99,999.999 使用 1 位小数
  • 从 100,000.01 到 999,999.99 使用 1 位小数
  • 从 1,000,000.1 到 9,999,999.9 使用 1 位小数
  • 从 10,000,000 不带小数到 99,999,999
  • 之后使用 10.001M 到 999.999M
  • 然后是 1.001B 到 999.999B
  • 然后是 1.001T 到 999.999T
  • 然后是简单的 > 999T
  • 千位和十进制分隔符使用用户的区域设置。
  • M、B、T 是本地化的
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:) 允许覆盖默认的 decimalSeparatorthousandSeparatorliterals

localizedString(from:) 使用

  • Locale.autoupdatingCurrentdecimalSeparatorthousandSeparator 进行设置
  • "amount_millions""amount_billions""amount_trillions" 等本地化字符串来自 literals。 您需要为不同的地区提供可本地化的字符串。
// Assuming russian locale and "amount_billions" = "Б"
formatter.localizedString(from: BigDecimal(BigInt("999999999000000000000"), 9)) // 999,999Б

安装

先决条件

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

手册

将此存储库作为子模块添加

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

获取依赖项

cd SwiftCryptoTokenFormatter
git submodule update --init

SwiftCryptoTokenFormatter 库的依赖项

  • BigInt

SwiftCryptoTokenFormatter.xcodeproj 拖放到您的项目中,并将 SwiftCryptoTokenFormatter 静态库链接到您的项目中。

CocoaPods

pod 'SwiftCryptoTokenFormatter'

Carthage

在您的 Cartfile 中: github "gnosis/SwiftCryptoTokenFormatter"

运行 carthage update 以构建框架,并将 SwiftCryptoTokenFormatter.framework 拖入您的 Xcode 项目。

Swift 包管理器

您可以使用 Swift 包管理器并在您自己的 Package.swift 中添加依赖项。

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

贡献者

许可

MIT 许可证(见 LICENSE 文件)。