Fix exception when converting to decimal values that reduce to N/1.

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.
This commit is contained in:
John Ralls 2020-11-07 16:22:18 -08:00
parent d03dc07b8d
commit 2290fa7c22
2 changed files with 13 additions and 1 deletions

View File

@ -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;

View File

@ -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());
}