diff --git a/.jazzy.yaml b/.jazzy.yaml index 93166a86..8797a21b 100644 --- a/.jazzy.yaml +++ b/.jazzy.yaml @@ -10,4 +10,4 @@ github_url: https://github.com/ArcBlock/arcblock-ios-sdk github_file_prefix: https://github.com/ArcBlock/arcblock-ios-sdk/tree/master exclude: - ArcBlockSDK/ABSDKCoreKit/Network/ABSDKPagination.swift -module_version: 0.11.14 +module_version: 0.11.15 diff --git a/ArcBlockSDK.podspec b/ArcBlockSDK.podspec index d8fcc525..4099bdc2 100644 --- a/ArcBlockSDK.podspec +++ b/ArcBlockSDK.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'ArcBlockSDK' - s.version = '0.11.14' + s.version = '0.11.15' s.summary = 'Used to integrate iOS apps with ArcBlock Platform.' # This description is used to generate tags and improve search results. diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift index e17753d6..ebd58bdc 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift @@ -28,7 +28,7 @@ import BigInt public extension BigUInt { static var MinFormattingDecimals: Int = 6 - /// 格式化余额展示 + /// 将BigUInt格式化成余额字符串展示 如: 1123456789000000000 -> 1.123456 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) @@ -54,6 +54,24 @@ public extension BigUInt { } } + /// 将BigUInt格式化成发送金额 用于显示在输入框中 不需要,分割数字 如 1123456789000000000000 -> 1123.456789 + /// + /// - Parameters: + /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) + func toSendString(decimals: Int? = 18, formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> String { + let realDecimals = decimals ?? 18 + var realFormattingDecimals = formattingDecimals + if realFormattingDecimals > Self.MinFormattingDecimals { + realFormattingDecimals = Self.MinFormattingDecimals + } + let amountStr = Web3.Utils.formatToPrecision(self, numberDecimals: realDecimals, formattingDecimals: realFormattingDecimals, decimalSeparator: ".", fallbackToScientific: false) ?? "0" + let formatter = NumberFormatter() + formatter.numberStyle = .none + formatter.maximumFractionDigits = realFormattingDecimals + formatter.roundingMode = .floor + return formatter.string(from: NSDecimalNumber(string: amountStr)) ?? "0" + } + // TODO: - 待废弃 func toAmountDouble(decimals: Int? = 18, formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> Double { let balance = self.toAmountString(decimals: decimals, formattingDecimals: formattingDecimals) diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift index 07d00329..1f321fc9 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift @@ -9,7 +9,7 @@ import Foundation import BigInt public extension Double { - /// 将解析完成的BigUInt格式化成余额展示 + /// 将double格式化成余额展示(这里的double非BigUInt格式) 如: 12.121234543546 -> 12.121234 10000 -> 10,000 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift index 20cef3ff..35d734cb 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift @@ -81,16 +81,23 @@ public extension String { return Web3.Utils.parseToBigUInt(self, decimals: decimals ?? 18) ?? BigUInt(0) } - /// 将解析完成的BigUInt格式化成余额展示 + /// 将字符串格式化成余额展示(这里的字符串非BigUInt格式) 如: "12.121234543546" -> 12.121234 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) func toAmountString(formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> String { - let balance = Double(self) - return balance?.toAmountString(formattingDecimals: formattingDecimals) ?? "0" + guard !isEmpty else { + return "0" + } + let realFormattingDecimals = min(formattingDecimals, BigUInt.MinFormattingDecimals) + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + formatter.maximumFractionDigits = realFormattingDecimals + formatter.roundingMode = .floor + return formatter.string(from: NSDecimalNumber(string: self)) ?? "0" } - /// 将未解析的BigUInt格式化成余额展示 + /// 将字符串格式化成余额展示(这里的字符串为BigUInt格式) 如: "1123456789000000000" -> 1123456789000000000 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) diff --git a/ArcBlockSDKTests/AmountSpec.swift b/ArcBlockSDKTests/AmountSpec.swift index 1eb3dbcb..eebd138f 100644 --- a/ArcBlockSDKTests/AmountSpec.swift +++ b/ArcBlockSDKTests/AmountSpec.swift @@ -43,6 +43,21 @@ class AmountSpec: QuickSpec { }) } + describe("BigUInt To Send") { + it("works", closure: { + expect(BigUInt("1123456789123456789").toSendString()).to(equal("1.123456")) + expect(BigUInt("1123456789000000000").toSendString()).to(equal("1.123456")) + expect(BigUInt("1123456789000000000000").toSendString()).to(equal("1123.456789")) + expect(BigUInt("1123400000000000000").toSendString()).to(equal("1.1234")) + expect(BigUInt("1000000000000000000").toSendString()).to(equal("1")) + expect(BigUInt("1000000000000000000000").toSendString()).to(equal("1000")) + expect(BigUInt("123456").toSendString(decimals: 5)).to(equal("1.23456")) + expect(BigUInt("100023456").toSendString(decimals: 5)).to(equal("1000.23456")) + expect(BigUInt("100023450").toSendString(decimals: 5)).to(equal("1000.2345")) + expect(BigUInt("").toSendString(decimals: 5)).to(equal("0")) + }) + } + describe("BigUInt String To Amount") { it("works", closure: { expect("1123456789000000000".toAmountString(decimals: 18)).to(equal("1.123456")) @@ -65,6 +80,8 @@ class AmountSpec: QuickSpec { expect("1000.2345".toAmountString()).to(equal("1,000.2345")) expect("1000.2345".toAmountString(formattingDecimals: 1)).to(equal("1,000.2")) expect("10.0".toAmountString()).to(equal("10")) + expect(".1".toAmountString()).to(equal("0.1")) + expect(".".toAmountString()).to(equal("0")) expect("".toAmountString()).to(equal("0")) }) } diff --git a/CHANGELOG.md b/CHANGELOG.md index b8915094..eb1d7ce8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.11.15 (November 24, 2021) + - send amount fix + - Merge pull request #86 from ArcBlock/BigUintFix + ## 0.11.14 (November 23, 2021) - BigUintFix diff --git a/version b/version index a95c45d4..4aa09069 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.11.14 +0.11.15