Skip to content

Commit

Permalink
Merge pull request #89 from ArcBlock/BigUintReviewFix
Browse files Browse the repository at this point in the history
BigUInt Review Fix
  • Loading branch information
karthuszY authored Dec 1, 2021
2 parents 1b052e6 + d0857e4 commit 186d15e
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .jazzy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ArcBlockSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 16 additions & 18 deletions ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,47 @@ 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)
}
}

/// 将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
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
}

Expand Down
15 changes: 12 additions & 3 deletions ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
9 changes: 4 additions & 5 deletions ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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)
}
}
10 changes: 5 additions & 5 deletions ArcBlockSDKTests/AmountSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
})
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.15
0.11.16

0 comments on commit 186d15e

Please sign in to comment.