truncate two decimal places without risking float rounding issues

This commit is contained in:
Alfred Kamanda 2025-06-24 14:55:09 +03:00
parent bc48dddd99
commit 96f6ca7d08
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -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)