commit 345e17aa34550060e3653b1c7bf27be12b2b1fc9
parent a0134110ff68746513e86dbdaf7eaccfdc673be6
Author: Marc Stibane <marc@taler.net>
Date: Thu, 20 Aug 2026 19:22:03 +0200
AI: normalize amounts
Diffstat:
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/taler-swift/Sources/taler-swift/Amount.swift b/taler-swift/Sources/taler-swift/Amount.swift
@@ -163,17 +163,21 @@ public final class Amount: Codable, Hashable, @unchecked Sendable, CustomStringC
/// The string representation of the value, formatted as "`integer`.`fraction`",
/// no trailing zeroes, no group separator.
public var valueStr: String {
- if fraction == 0 {
- return String(integer)
+ // Render a normalized copy: a fraction >= fractionalBase() would otherwise
+ // produce extra leading digits, e.g. fraction 4_000_000_000 as "0.400"
+ // instead of "40". Fall back to self when normalization is impossible.
+ let this = (try? normalizedCopy()) ?? self
+ if this.fraction == 0 {
+ return String(this.integer)
} else {
- var frac = UInt64(fraction)
+ var frac = UInt64(this.fraction)
let base = UInt64(fractionalBase())
var fracStr = ""
while (frac > 0) {
fracStr += String(frac / ( base / 10))
frac = (frac * 10) % base
}
- return "\(integer)\(Self.decimalSeparator)\(fracStr)"
+ return "\(this.integer)\(Self.decimalSeparator)\(fracStr)"
}
}