From cb4871af8d7c048e6c26a5e8ac310defebe6b89b Mon Sep 17 00:00:00 2001 From: DavidYu <654070281@qq.com> Date: Wed, 24 Nov 2021 10:41:21 +0800 Subject: [PATCH 1/3] send amount fix --- .../Extensions/BigUInt+Extension.swift | 18 ++++++++++++++++++ .../Extensions/String+Extension.swift | 11 +++++++++-- ArcBlockSDKTests/AmountSpec.swift | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift index e17753d6..6dae8290 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift @@ -54,6 +54,24 @@ public extension BigUInt { } } + /// 格式化发送金额 + /// + /// - 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/String+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift index 20cef3ff..3cd93061 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift @@ -86,8 +86,15 @@ public extension String { /// - 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格式化成余额展示 diff --git a/ArcBlockSDKTests/AmountSpec.swift b/ArcBlockSDKTests/AmountSpec.swift index 1eb3dbcb..0fe9cc8b 100644 --- a/ArcBlockSDKTests/AmountSpec.swift +++ b/ArcBlockSDKTests/AmountSpec.swift @@ -43,6 +43,20 @@ 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("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 +79,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")) }) } From 855e419fc843ee3703e5e41e36b7c585af632819 Mon Sep 17 00:00:00 2001 From: DavidYu <654070281@qq.com> Date: Wed, 24 Nov 2021 10:46:41 +0800 Subject: [PATCH 2/3] bump version --- .jazzy.yaml | 2 +- ArcBlockSDK.podspec | 2 +- CHANGELOG.md | 4 ++++ version | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) 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/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 From 87ad8bda1a466c2f8ed5030a0e1622ad39b6d1fb Mon Sep 17 00:00:00 2001 From: DavidYu <654070281@qq.com> Date: Wed, 24 Nov 2021 10:58:41 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B3=A8=E9=87=8Afix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift | 4 ++-- ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift | 2 +- ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift | 4 ++-- ArcBlockSDKTests/AmountSpec.swift | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift index 6dae8290..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,7 +54,7 @@ public extension BigUInt { } } - /// 格式化发送金额 + /// 将BigUInt格式化成发送金额 用于显示在输入框中 不需要,分割数字 如 1123456789000000000000 -> 1123.456789 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, 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 3cd93061..35d734cb 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift @@ -81,7 +81,7 @@ public extension String { return Web3.Utils.parseToBigUInt(self, decimals: decimals ?? 18) ?? BigUInt(0) } - /// 将解析完成的BigUInt格式化成余额展示 + /// 将字符串格式化成余额展示(这里的字符串非BigUInt格式) 如: "12.121234543546" -> 12.121234 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) @@ -97,7 +97,7 @@ public extension String { 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 0fe9cc8b..eebd138f 100644 --- a/ArcBlockSDKTests/AmountSpec.swift +++ b/ArcBlockSDKTests/AmountSpec.swift @@ -47,6 +47,7 @@ class AmountSpec: QuickSpec { 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"))