@@ -1500,18 +1500,40 @@ gnc_wrap_text_with_bidi_ltr_isolate (const char* text)
1500
1500
/* *******************************************************************\
1501
1501
********************************************************************/
1502
1502
1503
+ using StrStrVec = std::vector<std::pair<std::string, std::string>>;
1504
+
1505
+ static std::string
1506
+ string_replace_substring (const std::string& input, const StrStrVec& replacements)
1507
+ {
1508
+ std::string result = input;
1509
+ for (const auto & [key, val] : replacements)
1510
+ for (auto pos = result.find (key); pos != std::string::npos; pos = result.find (key))
1511
+ result.replace (pos, key.length (), val);
1512
+ return result;
1513
+ }
1514
+
1503
1515
static std::string
1504
1516
number_to_words (double val, int64_t denom)
1505
1517
{
1506
1518
double int_part;
1507
1519
const int frac_part = std::round (std::modf (std::fabs (val), &int_part) * denom);
1508
- const std::vector<std::string> tail =
1509
- { " " , _ (" and" ), " " , std::to_string (frac_part), " /" , std::to_string (denom) };
1510
- std::ostringstream ss;
1520
+ auto num_to_words = [](auto num)
1521
+ {
1522
+ std::ostringstream ss;
1523
+ ss.imbue (gnc_get_boost_locale ());
1524
+ ss << boost::locale::as::spellout << num;
1525
+ return ss.str ();
1526
+ };
1527
+ StrStrVec replacements = {
1528
+ {" {int_part}" , num_to_words (int_part)},
1529
+ {" {frac_part}" , num_to_words (frac_part)},
1530
+ {" {value}" , num_to_words (val)},
1531
+ {" {int_part_num}" , std::to_string (int_part)},
1532
+ {" {frac_part_num}" , std::to_string (frac_part)},
1533
+ {" {denom_num}" , std::to_string (denom)},
1534
+ };
1511
1535
1512
- ss.imbue (gnc_get_boost_locale ());
1513
- ss << boost::locale::as::spellout << int_part;
1514
- return std::accumulate (tail.begin (), tail.end (), ss.str ());
1536
+ return string_replace_substring (" {int_part} and {frac_part_num}/{denom_num}" , replacements);
1515
1537
}
1516
1538
1517
1539
#ifdef _MSC_VER
0 commit comments