[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-taler-ios] 07/11: intValue, fracValue, valueAsTuple
From: |
gnunet |
Subject: |
[taler-taler-ios] 07/11: intValue, fracValue, valueAsTuple |
Date: |
Tue, 24 Oct 2023 22:45:23 +0200 |
This is an automated email from the git hooks/post-receive script.
marc-stibane pushed a commit to branch master
in repository taler-ios.
commit 1bb30945a8af10d8331aae719a4f452d36b5d251
Author: Marc Stibane <marc@taler.net>
AuthorDate: Tue Oct 24 21:29:01 2023 +0200
intValue, fracValue, valueAsTuple
---
taler-swift/Sources/taler-swift/Amount.swift | 42 +++++++++++++++++++---------
1 file changed, 29 insertions(+), 13 deletions(-)
diff --git a/taler-swift/Sources/taler-swift/Amount.swift
b/taler-swift/Sources/taler-swift/Amount.swift
index ea952a8..25706cf 100644
--- a/taler-swift/Sources/taler-swift/Amount.swift
+++ b/taler-swift/Sources/taler-swift/Amount.swift
@@ -50,12 +50,12 @@ public final class Amount: Codable, Hashable, @unchecked
Sendable, CustomStringC
/// The size of `integer` in relation to `fraction`.
private static let fractionalBase: UInt32 = 100000000
- /// The greatest number of decimal digits that can be represented.
+ /// The greatest number of fractional digits that can be represented.
private static let fractionalBaseDigits: UInt = 8
- /// The currency of the amount.
- var currency: String
-
+ /// The currency of the amount. Cannot be changed later
+ private let currency: String
+
/// The integer value of the amount (number to the left of the decimal
point).
var integer: UInt64
@@ -73,18 +73,34 @@ public final class Amount: Codable, Hashable, @unchecked
Sendable, CustomStringC
}
}
- /// The floating point representation of the value
+ /// The floating point representation of the integer.
+ public var intValue: Double {
+ Double(integer)
+ }
+
+ /// The floating point representation of the fraction.
+ public var fracValue: Double {
+ let oneThousand = 1000.0
+ let base = Double(Amount.fractionalBase) / oneThousand
+ let thousandths = Double(fraction) / base
+ return thousandths / oneThousand
+ }
+
+ /// The floating point representation of the value.
+ /// Be careful, the value might exceed 15 digits which is the limit for
Double.
+ /// When more significant digits are needed, use valueAsTuple.
public var value: Double {
- let value = Double(integer)
- if fraction == 0 {
- return value
- } else {
- let thousandths = Double(fraction / (Amount.fractionalBase / 1000))
- return value + (thousandths / 1000.0)
- }
+ fraction == 0 ? intValue
+ : intValue + fracValue
+ }
+
+ /// The tuple representation of the value.
+ public var valueAsTuple: (Double, Double) {
+ (intValue, fracValue)
}
- /// The string representation of the value, formatted as
"`integer`.`fraction`".
+ /// The string representation of the value, formatted as
"`integer`.`fraction`",
+ /// no trailing zeroes, no group separator.
public var valueStr: String {
var decimalSeparator = "."
// if let currencySpecification { // TODO: use locale
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [taler-taler-ios] branch master updated (a0c6bdb -> bf7f8e3), gnunet, 2023/10/24
- [taler-taler-ios] 01/11: Assets cleanup, gnunet, 2023/10/24
- [taler-taler-ios] 02/11: Native Logging for wallet-core (QuickJS), gnunet, 2023/10/24
- [taler-taler-ios] 03/11: Logger, gnunet, 2023/10/24
- [taler-taler-ios] 05/11: Only call DetailsForAmount if non-zero, gnunet, 2023/10/24
- [taler-taler-ios] 10/11: CharacterSet+contains, gnunet, 2023/10/24
- [taler-taler-ios] 11/11: Layout for Pending - wip, gnunet, 2023/10/24
- [taler-taler-ios] 04/11: ExchangeRowView, gnunet, 2023/10/24
- [taler-taler-ios] 08/11: Cleanup, gnunet, 2023/10/24
- [taler-taler-ios] 09/11: DD51 - CurrencySpecification, gnunet, 2023/10/24
- [taler-taler-ios] 07/11: intValue, fracValue, valueAsTuple,
gnunet <=
- [taler-taler-ios] 06/11: Fix for NavigationView popping back, gnunet, 2023/10/24