From 0a69e04229bacda9a9febbdf802892c5b4c38c49 Mon Sep 17 00:00:00 2001 From: Alfred Kamanda Date: Thu, 29 Jan 2026 17:00:24 +0300 Subject: [PATCH] added helper functions to add scaled down balances --- store/vouchers.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/store/vouchers.go b/store/vouchers.go index efb59ed..dc849c6 100644 --- a/store/vouchers.go +++ b/store/vouchers.go @@ -225,3 +225,46 @@ func FormatVoucherList(ctx context.Context, symbolsData, balancesData string) [] } return combined } + +// AddDecimalStrings adds two decimal numbers represented as strings +// and returns the result as a string without losing precision. +func AddDecimalStrings(a, b string) string { + x, ok := new(big.Rat).SetString(a) + if !ok { + x = new(big.Rat) + } + + y, ok := new(big.Rat).SetString(b) + if !ok { + y = new(big.Rat) + } + + x.Add(x, y) + + // Convert back to string without scientific notation + return x.FloatString(maxDecimalPlaces(x, y)) +} + +// maxDecimalPlaces ensures we preserve enough decimal precision +func maxDecimalPlaces(rats ...*big.Rat) int { + max := 0 + for _, r := range rats { + if r == nil { + continue + } + if d := decimalPlaces(r); d > max { + max = d + } + } + return max +} + +func decimalPlaces(r *big.Rat) int { + s := r.FloatString(18) + for i := len(s) - 1; i >= 0; i-- { + if s[i] == '.' { + return len(s) - i - 1 + } + } + return 0 +}