mirror of
https://github.com/lov3b/ecb-rates.git
synced 2025-02-22 09:50:10 +01:00
Rounding
This commit is contained in:
parent
d842fb786c
commit
4b36904f3a
@ -41,6 +41,10 @@ pub struct Cli {
|
||||
#[arg(long = "invert", short = 'i')]
|
||||
pub should_invert: bool,
|
||||
|
||||
//// Max decimals to keep in price.
|
||||
#[arg(long = "max-decimals", short = 'd', default_value_t = 5)]
|
||||
pub max_decimals: u8,
|
||||
|
||||
/// Amount of data
|
||||
#[arg(value_enum, default_value_t = Resolution::TODAY, long="resolution", short='r')]
|
||||
pub resolution: Resolution,
|
||||
|
@ -7,7 +7,7 @@ use ecb_rates::cli::{Cli, FormatOption};
|
||||
use ecb_rates::models::ExchangeRateResult;
|
||||
use ecb_rates::parsing::parse;
|
||||
use ecb_rates::table::{TableRef, TableTrait as _};
|
||||
use ecb_rates::utils_calc::{change_perspective, filter_currencies, invert_rates};
|
||||
use ecb_rates::utils_calc::{change_perspective, filter_currencies, invert_rates, round};
|
||||
|
||||
async fn get_and_parse(url: impl IntoUrl) -> anyhow::Result<Vec<ExchangeRateResult>> {
|
||||
let client = Client::new();
|
||||
@ -84,6 +84,8 @@ async fn main() -> ExitCode {
|
||||
invert_rates(&mut parsed);
|
||||
}
|
||||
|
||||
round(&mut parsed, cli.max_decimals);
|
||||
|
||||
if !cli.currencies.is_empty() {
|
||||
let currencies = cli
|
||||
.currencies
|
||||
|
@ -36,7 +36,7 @@ pub fn helper_table_print<T: TableGet>(
|
||||
|
||||
for (left, right) in table.get_rows().iter() {
|
||||
let left_str = left.as_ref();
|
||||
let right_str = format!("{:.5}", right);
|
||||
let right_str = right.to_string();
|
||||
let padding_amount = width.saturating_sub(left_str.len() + right_str.len());
|
||||
let padding = " ".repeat(padding_amount);
|
||||
writeln!(f, "{}{}{}", left_str.bold().green(), padding, right_str)?;
|
||||
|
@ -44,3 +44,13 @@ pub fn invert_rates(exchange_rate_results: &mut [ExchangeRateResult]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn round(exchange_rate_results: &mut [ExchangeRateResult], max_decimals: u8) {
|
||||
let power = 10.0_f64.powf(max_decimals as f64);
|
||||
for rate_res in exchange_rate_results {
|
||||
for (_, iter_rate) in rate_res.rates.iter_mut() {
|
||||
let more = iter_rate.deref() * power;
|
||||
*iter_rate = more.round() / power;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user