From bfb4ca052a42af1ff9b4695c06ea0c45a906b45a Mon Sep 17 00:00:00 2001 From: Dave Peticolas Date: Wed, 8 Nov 2000 10:53:39 +0000 Subject: [PATCH] More numerics conversions. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3136 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/SplitLedger.c | 41 ++++++++++++ src/gnc-ui-util.c | 122 ++++++++++++++---------------------- src/gnome/gnc-amount-edit.c | 3 +- src/gnome/window-register.c | 7 ++- src/register/pricecell.c | 1 + src/register/splitreg.c | 3 + 6 files changed, 96 insertions(+), 81 deletions(-) diff --git a/src/SplitLedger.c b/src/SplitLedger.c index d4ee4396ff..ba06f45e7f 100644 --- a/src/SplitLedger.c +++ b/src/SplitLedger.c @@ -652,6 +652,42 @@ gnc_split_get_quantity_denom (Split *split) return denom; } +static void +sr_set_cell_fractions (SplitRegister *reg, Split *split) +{ + SRInfo *info = xaccSRGetInfo(reg); + Account *account; + + account = xaccSplitGetAccount (split); + + if (account == NULL) + account = info->default_source_account; + + if (account) + { + xaccSetPriceCellFraction (reg->sharesCell, + xaccAccountGetSecuritySCU (account)); + xaccSetPriceCellFraction (reg->debitCell, + xaccAccountGetCurrencySCU (account)); + xaccSetPriceCellFraction (reg->creditCell, + xaccAccountGetCurrencySCU (account)); + + return; + } + + { + const gnc_commodity *commodity; + int fraction; + + xaccSetPriceCellFraction (reg->sharesCell, 10000); + + commodity = gnc_locale_default_currency (); + fraction = gnc_commodity_get_fraction (commodity); + + xaccSetPriceCellFraction (reg->debitCell, fraction); + xaccSetPriceCellFraction (reg->creditCell, fraction); + } +} /* ======================================================== */ /* This callback gets called when the user clicks on the gui @@ -770,7 +806,10 @@ LedgerMoveCursor (Table *table, VirtualLocation *p_new_virt_loc) /* if the register was reloaded, then everything should be fine :) * otherwise, we may need to change some visibility settings. */ if (saved) + { + sr_set_cell_fractions (reg, new_split); return; + } /* in the mult-line and dynamic modes, we need to hide the old * and show the new. */ @@ -794,6 +833,8 @@ LedgerMoveCursor (Table *table, VirtualLocation *p_new_virt_loc) info->cursor_hint_cursor_class = new_class; info->hint_set_by_traverse = FALSE; + + sr_set_cell_fractions (reg, new_split); } /* This function determines if auto-completion is appropriate and, diff --git a/src/gnc-ui-util.c b/src/gnc-ui-util.c index 44bf7e35d5..49073a3a2c 100644 --- a/src/gnc-ui-util.c +++ b/src/gnc-ui-util.c @@ -132,7 +132,7 @@ gnc_locale_default_currency(void) /* Return the number of decimal places for this locale. */ int -gnc_locale_decimal_places( void ) +gnc_locale_decimal_places (void) { static gboolean got_it = FALSE; static int places; @@ -152,42 +152,6 @@ gnc_locale_decimal_places( void ) } -/* utility function to convert floating point value to a string */ -static int -util_fptostr(char *buf, double val, int prec) -{ - int i; - char formatString[10]; - char prefix[] = "%0."; - char postfix[] = "f"; - - /* This routine can only handle precision between 0 and 9, so - * clamp precision to that range */ - if (prec > 9) prec = 9; - if (prec < 0) prec = 0; - - /* Make sure that the output does not resemble "-0.00" by forcing - * val to 0.0 when we have a very small negative number */ - if ((val <= 0.0) && (val > -pow(0.1, prec+1) * 5.0)) - val = 0.0; - - /* Create a format string to pass into sprintf. By doing this, - * we can get sprintf to convert the number to a string, rather - * than maintaining conversion code ourselves. */ - i = 0; - strcpy(&formatString[i], prefix); - i += strlen(prefix); - formatString[i] = '0' + prec; /* add prec to ASCII code for '0' */ - i += 1; - strcpy(&formatString[i], postfix); - i += strlen(postfix); - - sprintf(buf, formatString, val); - - return strlen(buf); -} - - static char * gnc_stpcpy (char *dest, const char *src) { @@ -225,21 +189,25 @@ gnc_default_print_info (gboolean use_symbol) } static gboolean -is_decimal_fraction (int fraction, guint8 *max_decimal_places) +is_decimal_fraction (int fraction, guint8 *max_decimal_places_p) { - if (fraction % 10 != 0) + guint8 max_decimal_places = 0; + + if (fraction <= 0) return FALSE; - if (max_decimal_places == NULL) - return TRUE; - - *max_decimal_places = 0; while (fraction != 1) { + if (fraction % 10 != 0) + return FALSE; + fraction = fraction / 10; - *max_decimal_places += 1; + max_decimal_places += 1; } + if (max_decimal_places_p) + *max_decimal_places_p = max_decimal_places; + return TRUE; } @@ -428,8 +396,7 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info) } /* print the absolute value */ - if (gnc_numeric_negative_p (val)) - val = gnc_numeric_neg (val); + val = gnc_numeric_abs (val); /* calculate the integer part and the remainder */ whole = gnc_numeric_create (val.num / val.denom, 1); @@ -516,9 +483,9 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info) { val = gnc_numeric_reduce (val); - printf (temp_buf, " + %lld / %lld", - (long long) val.num, - (long long) val.denom); + sprintf (temp_buf, " + %lld / %lld", + (long long) val.num, + (long long) val.denom); strcat (buf, temp_buf); } @@ -575,7 +542,7 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info) } int -DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) +xaccSPrintAmount (char * bufp, gnc_numeric val, GNCPrintAmountInfo info) { struct lconv *lc; @@ -589,13 +556,11 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) gboolean print_sign = TRUE; - if (!bufp) return 0; + if (!bufp) + return 0; lc = gnc_localeconv(); - if (DEQ(val, 0.0)) - val = 0.0; - if (info.use_symbol) { if (gnc_commodity_equiv (info.commodity, gnc_locale_default_currency ())) @@ -619,7 +584,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) } else { - if (val < 0.0) + if (gnc_numeric_negative_p (val)) { cs_precedes = lc->n_cs_precedes; sep_by_space = lc->n_sep_by_space; @@ -631,7 +596,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) } } - if (val < 0.0) + if (gnc_numeric_negative_p (val)) { sign = lc->negative_sign; sign_posn = lc->n_sign_posn; @@ -642,7 +607,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) sign_posn = lc->p_sign_posn; } - if ((val == 0.0) || (sign == NULL) || (sign[0] == 0)) + if (gnc_numeric_zero_p (val) || (sign == NULL) || (sign[0] == 0)) print_sign = FALSE; /* See if we print sign now */ @@ -673,10 +638,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) bufp = gnc_stpcpy(bufp, "("); /* Now print the value */ - { - gnc_numeric n = double_to_gnc_numeric (ABS (val), 10000, GNC_RND_ROUND); - bufp += PrintAmountInternal(bufp, n, &info); - } + bufp += PrintAmountInternal(bufp, val, &info); /* Now see if we print parentheses */ if (print_sign && (sign_posn == 0)) @@ -710,32 +672,40 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) } const char * -DxaccPrintAmount (double val, GNCPrintAmountInfo info) +xaccPrintAmount (gnc_numeric val, GNCPrintAmountInfo info) { - /* hack alert -- this is not thread safe ... */ - static char buf[1024]; + /* hack alert -- this is not thread safe ... */ + static char buf[1024]; - DxaccSPrintAmount (buf, val, info); + xaccSPrintAmount (buf, val, info); - /* its OK to return buf, since we declared it static */ - return buf; -} - -int -xaccSPrintAmount (char * bufp, gnc_numeric val, GNCPrintAmountInfo info) -{ - return DxaccSPrintAmount (bufp, gnc_numeric_to_double (val), info); + /* its OK to return buf, since we declared it static */ + return buf; } const char * -xaccPrintAmount (gnc_numeric val, GNCPrintAmountInfo info) +DxaccPrintAmount (double dval, GNCPrintAmountInfo info) { - return DxaccPrintAmount (gnc_numeric_to_double (val), info); + gnc_numeric val; + + val = double_to_gnc_numeric (ABS (dval), 10000, GNC_RND_ROUND); + + return xaccPrintAmount (val, info); +} + +int +DxaccSPrintAmount (char * bufp, double dval, GNCPrintAmountInfo info) +{ + gnc_numeric val; + + val = double_to_gnc_numeric (ABS (dval), 10000, GNC_RND_ROUND); + + return xaccSPrintAmount (bufp, val, info); } /********************************************************************\ - * DxaccParseAmount * + * xaccParseAmount * * parses amount strings using locale data * * * * Args: in_str -- pointer to string rep of num * diff --git a/src/gnome/gnc-amount-edit.c b/src/gnome/gnc-amount-edit.c index 468134993c..44ef56d25b 100644 --- a/src/gnome/gnc-amount-edit.c +++ b/src/gnome/gnc-amount-edit.c @@ -343,8 +343,7 @@ gnc_amount_edit_set_amount (GNCAmountEdit *gae, gnc_numeric amount) gae->amount = amount; gae->need_to_parse = FALSE; - amount_string = DxaccPrintAmount (gnc_numeric_to_double(amount), - gae->print_info); + amount_string = xaccPrintAmount (amount, gae->print_info); gtk_entry_set_text (GTK_ENTRY (gae->amount_entry), amount_string); } diff --git a/src/gnome/window-register.c b/src/gnome/window-register.c index 0464ab46bc..d01e801c59 100644 --- a/src/gnome/window-register.c +++ b/src/gnome/window-register.c @@ -1053,7 +1053,7 @@ print_check_cb(GtkWidget * widget, gpointer data) const char * payee; const char * memo; - double amount; + gnc_numeric amount; time_t date; SCM print_check = gh_eval_str("gnc:print-check"); @@ -1062,13 +1062,14 @@ print_check_cb(GtkWidget * widget, gpointer data) gh_procedure_p(print_check)) { payee = xaccTransGetDescription(trans); - amount = DxaccSplitGetValue(split); + amount = xaccSplitGetValue(split); + amount = gnc_numeric_abs (amount); date = xaccTransGetDate(trans); memo = xaccSplitGetMemo(split); gh_apply(print_check, SCM_LIST4(gh_str02scm(payee), - gh_double2scm(ABS(amount)), + gh_double2scm(gnc_numeric_to_double (amount)), gh_ulong2scm(date), gh_str02scm(memo))); } diff --git a/src/register/pricecell.c b/src/register/pricecell.c index 7118f5e8e2..b6bbea44d8 100644 --- a/src/register/pricecell.c +++ b/src/register/pricecell.c @@ -40,6 +40,7 @@ #include "gnc-common.h" #include "gnc-exp-parser.h" #include "gnc-engine-util.h" +#include "gnc-numeric.h" #include "gnc-ui-util.h" #include "basiccell.h" diff --git a/src/register/splitreg.c b/src/register/splitreg.c index 63a4dfc6d1..cdfd504655 100644 --- a/src/register/splitreg.c +++ b/src/register/splitreg.c @@ -798,6 +798,9 @@ xaccInitSplitRegister (SplitRegister *reg, /* by default, don't blank zeros on the price cells. */ xaccSetPriceCellBlankZero(reg->priceCell, FALSE); + /* Use 5 decimal places for prices */ + xaccSetPriceCellFraction (reg->priceCell, 100000); + /* The reconcile cell should only be entered with the pointer, and * only then when the user clicks directly on the cell. */ reg->recnCell->cell.input_output |= XACC_CELL_ALLOW_EXACT_ONLY;