From 96f6ca7d087871dad8c0bcb2ce818cd6b8094763 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 24 Jun 2025 14:55:09 +0300 Subject: [PATCH] truncate two decimal places without risking float rounding issues --- handlers/application/menuhandler.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 8b4a30b..894d2c7 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -1861,8 +1861,11 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b return res, nil } - // Format the amount with 2 decimal places before saving - formattedAmount := fmt.Sprintf("%.2f", inputAmount) + // Format the amount to 2 decimal places before saving (truncated) + cents := int((inputAmount + 1e-9) * 100) + truncated := float64(cents) / 100 + formattedAmount := fmt.Sprintf("%.2f", truncated) + err = store.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount)) if err != nil { logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", storedb.DATA_AMOUNT, "value", formattedAmount, "error", err)