Skip to content

Commit

Permalink
feat: display usd values rounded to 2 decimal places; bump to 0.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ndavd committed Jan 25, 2025
1 parent 3b41f0f commit 07f129c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "book-of-profits"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
authors = ["Nuno David <[email protected]>"]
license = "MIT"
Expand Down
10 changes: 7 additions & 3 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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::<Vec<_>>();
Expand All @@ -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(())
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/float.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod spinner;
pub mod support_option;
pub mod table;
pub mod text;
pub mod float;

0 comments on commit 07f129c

Please sign in to comment.