From ed5886921bcce0d1a261a37aa83bf135259b7d21 Mon Sep 17 00:00:00 2001 From: Rafael Ascensão Date: Sat, 25 Sep 2021 23:47:08 +0100 Subject: Fix --time-colon for negative time amounts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the current formula works for positive numbers, negative numbers are incorrectly represented. One of the issues comes from the fact that floor(x) < x for every x. `amount_t precision` will always be a non negative number and the code that attempts to fix the issue for negative number will never run. If we truncate the number instead, the current formula works for both positive and negative numbers without making negative numbers a corner case. So let's do that. Signed-off-by: Rafael Ascensão --- src/amount.cc | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/amount.cc') diff --git a/src/amount.cc b/src/amount.cc index 6baa903c..4eacdd09 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -733,13 +733,9 @@ void amount_t::in_place_unreduce() if (shifted) { if (("h" == comm->symbol() || "m" == comm->symbol()) && commodity_t::time_colon_by_default) { - amount_t floored = tmp.floored(); - amount_t precision = tmp - floored; - if (precision < 0.0) { - precision += 1.0; - floored -= 1.0; - } - tmp = floored + (precision * (comm->smaller()->number() / 100.0)); + double truncated = trunc(tmp.to_double()); + double precision = tmp.to_double() - truncated; + tmp = truncated + (precision * (comm->smaller()->number() / 100.0)); } *this = tmp; -- cgit v1.2.3