From 2985b9b37b4299877030edcdb12f0cfc1de9572d Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Thu, 5 Dec 2024 00:05:21 +0100 Subject: [PATCH] Support comma separated thousands in monetary amount --- src/basic-objects/src/monetaryamount.cpp | 4 ++++ src/basic-objects/test/monetaryamount_test.cpp | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/basic-objects/src/monetaryamount.cpp b/src/basic-objects/src/monetaryamount.cpp index 4aa27405..a028a6b0 100644 --- a/src/basic-objects/src/monetaryamount.cpp +++ b/src/basic-objects/src/monetaryamount.cpp @@ -135,6 +135,10 @@ inline std::pair AmountIntegralFromStr(std:: if (ch == ' ') { break; } + if (ch == ',') { + // Comma can be used to separate thousands, we ignore it. + continue; + } throw exception("Amount string {} with invalid character {}", amountStr, ch); } diff --git a/src/basic-objects/test/monetaryamount_test.cpp b/src/basic-objects/test/monetaryamount_test.cpp index 71797159..262c1e64 100644 --- a/src/basic-objects/test/monetaryamount_test.cpp +++ b/src/basic-objects/test/monetaryamount_test.cpp @@ -767,6 +767,8 @@ TEST(MonetaryAmountTest, ExoticInput) { EXPECT_EQ(MonetaryAmount(" -.9 f&g "), MonetaryAmount("-0.9F&G")); + EXPECT_EQ(MonetaryAmount("2,708.89034514"), MonetaryAmount("2708.89034514")); + EXPECT_THROW(MonetaryAmount("--.9"), exception); }