Skip to content

Commit

Permalink
modify to output "number_in_words and nn/denom"
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Apr 11, 2024
1 parent 16f1a80 commit aa70ef3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions libgnucash/app-utils/gnc-ui-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include <unicode/listformatter.h>
#include <unicode/rbnf.h>

#include <numeric>

#include "qof.h"
#include "gnc-prefs.h"
#include "Account.h"
Expand Down Expand Up @@ -1498,26 +1500,33 @@ gnc_wrap_text_with_bidi_ltr_isolate (const char* text)
********************************************************************/

static std::string
number_to_words(double val)
number_to_words(double val, int64_t denom)
{
UErrorCode status{U_ZERO_ERROR};
icu::RuleBasedNumberFormat formatter{icu::URBNF_SPELLOUT, icu::Locale{}, status};
icu::UnicodeString result;
std::string words;
double int_part;
if (U_FAILURE(status))
{
PERR("Error creating formatter: %s", u_errorName(status));
return "";
}

formatter.format (std::fabs(val), result, status);
const int frac_part = std::round(std::modf (std::fabs(val), &int_part) * denom);
const std::vector<std::string> tail =
{ " ", _("and"), " ", std::to_string (frac_part), "/", std::to_string (denom) };

formatter.format (int_part, result, status);
if (U_FAILURE(status))
{
PERR("Error formatting number: %s", u_errorName(status));
return "";
}

result.toUTF8String(words);

words = std::accumulate (tail.begin(), tail.end(), words);
DEBUG ("Number %f in words: %s", val, words.c_str());

return words;
Expand All @@ -1534,7 +1543,7 @@ static double round(double x)
char*
numeric_to_words(gnc_numeric val)
{
return g_strdup(number_to_words (gnc_numeric_to_double(val)).c_str());
return g_strdup(number_to_words (gnc_numeric_to_double(val), gnc_numeric_denom(val)).c_str());
}

const char*
Expand Down

0 comments on commit aa70ef3

Please sign in to comment.