From 07f129c73d7fd7abfbeb1c61e8470bd64c0e3241 Mon Sep 17 00:00:00 2001 From: Nuno David Date: Sat, 25 Jan 2025 15:43:54 +0000 Subject: [PATCH] feat: display usd values rounded to 2 decimal places; bump to 0.3.4 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/repl/mod.rs | 10 +++++++--- src/utils/float.rs | 16 ++++++++++++++++ src/utils/mod.rs | 1 + 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/utils/float.rs diff --git a/Cargo.lock b/Cargo.lock index 8784b60..63fc338 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,7 +175,7 @@ dependencies = [ [[package]] name = "book-of-profits" -version = "0.3.3" +version = "0.3.4" dependencies = [ "age", "base58", diff --git a/Cargo.toml b/Cargo.toml index de63b92..d7456e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "book-of-profits" -version = "0.3.3" +version = "0.3.4" edition = "2021" authors = ["Nuno David "] license = "MIT" diff --git a/src/repl/mod.rs b/src/repl/mod.rs index e3147fd..6fd3feb 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -19,7 +19,10 @@ use crate::{ Chain, ChainOps, }, dexscreener, - utils::{retry::handle_retry_indexed, spinner::Spinner, table::Table, text::StylizedText}, + utils::{ + float::ExtendFloat, retry::handle_retry_indexed, spinner::Spinner, table::Table, + text::StylizedText, + }, }; static BOOK_OF_PROFITS: &str = "Book of Profits"; @@ -728,7 +731,7 @@ alias, if set. balance.chain.clone(), balance.token.symbol.clone(), balance.token.format(&balance.balance_native).to_string(), - balance.balance_usd.to_string(), + balance.balance_usd.round_to_fixed_string(2), ]) }) .collect::>(); @@ -750,7 +753,8 @@ alias, if set. relevant_balances.len(), relevant_balances .iter() - .fold(0.0, |sum, b| sum + b.balance_usd), + .fold(0.0, |sum, b| sum + b.balance_usd) + .round_to_fixed_string(2), ); Ok(()) } diff --git a/src/utils/float.rs b/src/utils/float.rs new file mode 100644 index 0000000..9f36c77 --- /dev/null +++ b/src/utils/float.rs @@ -0,0 +1,16 @@ +#![allow(dead_code)] + +pub trait ExtendFloat: num_traits::Float { + fn round_to_fixed(&self, decimals: u8) -> Self; + fn round_to_fixed_string(&self, decimals: u8) -> String; +} + +impl ExtendFloat for f64 { + fn round_to_fixed(&self, decimals: u8) -> Self { + let precision = 10.0_f64.powi(decimals as i32); + (self * precision).round() / precision + } + fn round_to_fixed_string(&self, decimals: u8) -> String { + format!("{:.*}", decimals as usize, self) + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a83eb3c..a816f5a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,3 +3,4 @@ pub mod spinner; pub mod support_option; pub mod table; pub mod text; +pub mod float;