Skip to content

Commit

Permalink
Fix exception when converting to decimal values that reduce to N/1.
Browse files Browse the repository at this point in the history
Before this the loop wouldn't terminate until the denominator had been
reduced to 0 and trying to create a GncRational with a 0 denominator
throws.
  • Loading branch information
jralls committed Nov 8, 2020
1 parent d03dc07 commit 2290fa7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libgnucash/engine/gnc-numeric.cpp
Expand Up @@ -368,7 +368,7 @@ GncNumeric::to_decimal(unsigned int max_places) const
rr_num *= factor;
rr_den *= factor;
}
while (!rr_num.isZero() && rr_num % 10 == 0)
while (!rr_num.isZero() && rr_num > 9 && rr_den > 9 && rr_num % 10 == 0)
{
rr_num /= 10;
rr_den /= 10;
Expand Down
12 changes: 12 additions & 0 deletions libgnucash/engine/test/gtest-gnc-numeric.cpp
Expand Up @@ -548,4 +548,16 @@ TEST(gnc_numeric_functions, test_conversion_to_decimal)
EXPECT_NO_THROW(r = c.to_decimal());
EXPECT_EQ(27434842, r.num());
EXPECT_EQ(100, r.denom());
GncNumeric d(11900000000, 85000000);
EXPECT_NO_THROW(r = d.to_decimal());
EXPECT_EQ(140, r.num());
EXPECT_EQ(1, r.denom());
GncNumeric e(11050000000, 65000000);
EXPECT_NO_THROW(r = e.to_decimal());
EXPECT_EQ(170, r.num());
EXPECT_EQ(1, r.denom());
GncNumeric f(5000000000, 50000000 );
EXPECT_NO_THROW(r = f.to_decimal());
EXPECT_EQ(100, r.num());
EXPECT_EQ(1, r.denom());
}

0 comments on commit 2290fa7

Please sign in to comment.