Skip to content

Commit

Permalink
Merge pull request #87 from ArcBlock/BigUintFix
Browse files Browse the repository at this point in the history
send amount fix
  • Loading branch information
karthuszY authored Nov 24, 2021
2 parents e2e3a42 + 87ad8bd commit 1b052e6
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 9 deletions.
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.14
module_version: 0.11.15
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.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.
Expand Down
20 changes: 19 additions & 1 deletion ArcBlockSDK/ABSDKCoreKit/Extensions/BigUInt+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import BigInt
public extension BigUInt {
static var MinFormattingDecimals: Int = 6

/// 格式化余额展示
/// 将BigUInt格式化成余额字符串展示 如: 1123456789000000000 -> 1.123456
///
/// - Parameters:
/// - formattingDecimals: 保留的小数位 最终取Min(6, formattingDecimals)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ArcBlockSDK/ABSDKCoreKit/Extensions/Double+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions ArcBlockSDK/ABSDKCoreKit/Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions ArcBlockSDKTests/AmountSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
})
}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.14
0.11.15

0 comments on commit 1b052e6

Please sign in to comment.