From 37c95909dbebacfce08aef978857b8e96c6a9566 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:27:21 +1030 Subject: [PATCH] Reduce the epsilon value format to 5 decimal places --- src/move_money.rs | 10 +++++++++- src/overhead_allocation.rs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/move_money.rs b/src/move_money.rs index 35df9e3..d5e423e 100644 --- a/src/move_money.rs +++ b/src/move_money.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use itertools::Itertools; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, Serializer}; #[derive(Debug, Deserialize)] struct CsvMovementRule { @@ -86,9 +86,17 @@ pub struct CsvCost { pub account: String, #[serde(rename = "COSTCENTRE")] pub department: String, + #[serde(serialize_with = "round_serialize")] pub value: f64, } +fn round_serialize(x: &f64, s: S) -> Result +where + S: Serializer, +{ + s.serialize_f64((x * 100000.).round() / 100000.) +} + pub fn move_money( rules_reader: csv::Reader, lines_reader: csv::Reader, diff --git a/src/overhead_allocation.rs b/src/overhead_allocation.rs index dca0cea..ae3226b 100644 --- a/src/overhead_allocation.rs +++ b/src/overhead_allocation.rs @@ -388,7 +388,7 @@ where for cost in results { for department in cost.summed_department_costs { // Any consumers should assume missing cc/account value was 0 (we already ignore overhead, as they all 0 out) - if department.value != 0. { + if department.value > 0.00001 || department.value < -0.00001 { output.serialize(CsvCost { account: cost.account.clone(), department: department.department,