From 1260618e73cf48fad1861ee584429fc38a570772 Mon Sep 17 00:00:00 2001 From: DavidYu <654070281@qq.com> Date: Wed, 1 Dec 2021 15:26:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=B0=8F=E6=95=B0=E7=82=B9Review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/BigUInt+Extension.swift | 34 +++++++++---------- .../Extensions/Double+Extension.swift | 15 ++++++-- .../Extensions/String+Extension.swift | 9 +++-- ArcBlockSDKTests/AmountSpec.swift | 10 +++--- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift index ebd58bdc..96d8b9b6 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift @@ -32,25 +32,24 @@ public extension BigUInt { /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) - func toAmountString(decimals: Int? = 18, formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> String { - let realDecimals = decimals ?? 18 - var realFormattingDecimals = formattingDecimals - if realFormattingDecimals > Self.MinFormattingDecimals { - realFormattingDecimals = Self.MinFormattingDecimals + func toAmountString(decimals: Int? = 18) -> String { + var realDecimals = decimals ?? 18 + if realDecimals > BigUInt.MinFormattingDecimals { + realDecimals = BigUInt.MinFormattingDecimals } - let amountStr = Web3.Utils.formatToPrecision(self, numberDecimals: realDecimals, formattingDecimals: realFormattingDecimals, decimalSeparator: ".", fallbackToScientific: false) ?? "0" + let amountStr = Web3.Utils.formatToPrecision(self, numberDecimals: decimals ?? 18, formattingDecimals: realDecimals, decimalSeparator: ".", fallbackToScientific: false) ?? "0" let formatter = NumberFormatter() formatter.numberStyle = .decimal - formatter.maximumFractionDigits = realFormattingDecimals + formatter.maximumFractionDigits = realDecimals formatter.roundingMode = .floor return formatter.string(from: NSDecimalNumber(string: amountStr)) ?? "0" } func toAmountFormattedString(decimals: Int? = 18) -> String { if self >= "1".toBigUInt(decimals: decimals) { - return self.toAmountString(decimals: decimals, formattingDecimals: 2) + return self.toAmountString(decimals: decimals) } else { - return self.toAmountString(decimals: decimals, formattingDecimals: 8) + return self.toAmountString(decimals: decimals) } } @@ -58,23 +57,22 @@ 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 + func toSendString(decimals: Int? = 18) -> String { + var realDecimals = decimals ?? 18 + if realDecimals > BigUInt.MinFormattingDecimals { + realDecimals = BigUInt.MinFormattingDecimals } - let amountStr = Web3.Utils.formatToPrecision(self, numberDecimals: realDecimals, formattingDecimals: realFormattingDecimals, decimalSeparator: ".", fallbackToScientific: false) ?? "0" + let amountStr = Web3.Utils.formatToPrecision(self, numberDecimals: decimals ?? 18, formattingDecimals: realDecimals, decimalSeparator: ".", fallbackToScientific: false) ?? "0" let formatter = NumberFormatter() formatter.numberStyle = .none - formatter.maximumFractionDigits = realFormattingDecimals + formatter.maximumFractionDigits = realDecimals 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) + func toAmountDouble(decimals: Int? = 18) -> Double { + let balance = self.toAmountString(decimals: decimals) return Double(balance) ?? 0.0 } diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift index 1f321fc9..1a942494 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift @@ -9,15 +9,24 @@ import Foundation import BigInt public extension Double { + /// 格式化法币展示 保留2位小数 如 1000.2345 -> 1,000.23 + var formatCurrency: String { + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + formatter.maximumFractionDigits = 2 + formatter.minimumFractionDigits = 2 + formatter.roundingMode = .floor + return formatter.string(from: NSDecimalNumber(floatLiteral: self)) ?? "0" + } + /// 将double格式化成余额展示(这里的double非BigUInt格式) 如: 12.121234543546 -> 12.121234 10000 -> 10,000 /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) - func toAmountString(formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> String { - let realFormattingDecimals = min(formattingDecimals, BigUInt.MinFormattingDecimals) + func toAmountString() -> String { let formatter = NumberFormatter() formatter.numberStyle = .decimal - formatter.maximumFractionDigits = realFormattingDecimals + formatter.maximumFractionDigits = BigUInt.MinFormattingDecimals formatter.roundingMode = .floor return formatter.string(from: NSNumber(floatLiteral: self)) ?? "0" } diff --git a/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift index 35d734cb..86b73448 100644 --- a/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift +++ b/ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift @@ -85,14 +85,13 @@ public extension String { /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) - func toAmountString(formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> String { + func toAmountString() -> String { guard !isEmpty else { return "0" } - let realFormattingDecimals = min(formattingDecimals, BigUInt.MinFormattingDecimals) let formatter = NumberFormatter() formatter.numberStyle = .decimal - formatter.maximumFractionDigits = realFormattingDecimals + formatter.maximumFractionDigits = BigUInt.MinFormattingDecimals formatter.roundingMode = .floor return formatter.string(from: NSDecimalNumber(string: self)) ?? "0" } @@ -101,10 +100,10 @@ public extension String { /// /// - Parameters: /// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals) - func toAmountString(decimals: Int? = 18, formattingDecimals: Int = BigUInt.MinFormattingDecimals) -> String { + func toAmountString(decimals: Int? = 18) -> String { guard let balance = Web3.Utils.parseToBigUInt(self, decimals: 0) else { return "0" } - return balance.toAmountString(decimals: decimals, formattingDecimals: formattingDecimals) + return balance.toAmountString(decimals: decimals) } } diff --git a/ArcBlockSDKTests/AmountSpec.swift b/ArcBlockSDKTests/AmountSpec.swift index eebd138f..6d764df7 100644 --- a/ArcBlockSDKTests/AmountSpec.swift +++ b/ArcBlockSDKTests/AmountSpec.swift @@ -76,9 +76,9 @@ class AmountSpec: QuickSpec { expect("12.00".toAmountString()).to(equal("12")) expect("12.121234543546".toAmountString()).to(equal("12.121234")) expect("12.1212345435460000".toAmountString()).to(equal("12.121234")) - expect("12.1212345435460000".toAmountString(formattingDecimals: 2)).to(equal("12.12")) + expect("12.1212345435460000".toAmountString()).to(equal("12.121234")) + expect("1000.2345".toAmountString()).to(equal("1,000.2345")) 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")) @@ -91,12 +91,12 @@ class AmountSpec: QuickSpec { expect(12.00.toAmountString()).to(equal("12")) expect(12.121234543546.toAmountString()).to(equal("12.121234")) expect(12.1212345435460000.toAmountString()).to(equal("12.121234")) - expect(12.1212345435460000.toAmountString(formattingDecimals: 2)).to(equal("12.12")) + expect(12.1212345435460000.toAmountString()).to(equal("12.121234")) + expect(1000.2345.toAmountString()).to(equal("1,000.2345")) 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(0.123456789.toAmountString()).to(equal("0.123456")) - expect(0.123456789.toAmountString(formattingDecimals: 3)).to(equal("0.123")) + expect(0.123456789.toAmountString()).to(equal("0.123456")) expect(0.0.toAmountString()).to(equal("0")) expect(10000.toAmountString()).to(equal("10,000")) }) From cae817ff23618551f54b922b4dbf513c3ef957f3 Mon Sep 17 00:00:00 2001 From: DavidYu <654070281@qq.com> Date: Wed, 1 Dec 2021 15:38:06 +0800 Subject: [PATCH 2/3] bump version --- .jazzy.yaml | 2 +- ArcBlockSDK.podspec | 2 +- CHANGELOG.md | 3 +++ version | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.jazzy.yaml b/.jazzy.yaml index 8797a21b..daf5a340 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.15 +module_version: 0.11.16 diff --git a/ArcBlockSDK.podspec b/ArcBlockSDK.podspec index 4099bdc2..47122ca7 100644 --- a/ArcBlockSDK.podspec +++ b/ArcBlockSDK.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'ArcBlockSDK' - s.version = '0.11.15' + s.version = '0.11.16' 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 eb1d7ce8..3e90177f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.11.16 (December 01, 2021) + - 小数点Review + ## 0.11.15 (November 24, 2021) - send amount fix - Merge pull request #86 from ArcBlock/BigUintFix diff --git a/version b/version index 4aa09069..db07b54a 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.11.15 +0.11.16 From d0857e482f36528ea04d86ca8243d88375e8ebd9 Mon Sep 17 00:00:00 2001 From: DavidYu <654070281@qq.com> Date: Wed, 1 Dec 2021 15:46:46 +0800 Subject: [PATCH 3/3] ci fix --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ae712fd6..67e9c659 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ jobs: - name: Checkout uses: actions/checkout@v2 - name: Set Xcode Version - run: sudo xcode-select -s /Applications/Xcode_12.app + run: sudo xcode-select -s /Applications/Xcode_12.5.1.app - name: Install Dependency run: | pod install