From 8f67e58372615e70c4de7b7e8841aac2bb8afdbc Mon Sep 17 00:00:00 2001
From: Nicolas80 <nicolas.pmail@protonmail.com>
Date: Sun, 8 Dec 2024 19:37:36 +0100
Subject: [PATCH] Fixing precision issue when converting the u64 cents amount
 into f32. (clients/rust/gcli-v2s!39)

* Fixing precision issue when converting the u64 cents amount into f32.
Now composing the displayed value with integer part and fractional part separately (keeping left padding of 0 for the decimal part for the amount of decimals required)
---
 src/data.rs | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/data.rs b/src/data.rs
index d0462af..5dbc79d 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -122,11 +122,16 @@ impl Data {
 	}
 	// --- methods ---
 	pub fn format_balance(&self, amount: Balance) -> String {
-		let base: u32 = 10;
+		let base: u64 = 10;
+		let integer_part = amount / base.pow(self.token_decimals);
+		let fractional_part = amount % base.pow(self.token_decimals);
+
 		format!(
-			"{} {}",
-			(amount as f32) / (base.pow(self.token_decimals) as f32),
-			self.token_symbol
+			"{}.{:0left_padding$} {}",
+			integer_part,
+			fractional_part,
+			self.token_symbol,
+			left_padding = self.token_decimals as usize
 		)
 	}
 	// --- mutators ---
-- 
GitLab