More numerics conversions.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3136 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Dave Peticolas 2000-11-08 10:53:39 +00:00
parent 53df065d52
commit bfb4ca052a
6 changed files with 96 additions and 81 deletions

View File

@ -652,6 +652,42 @@ gnc_split_get_quantity_denom (Split *split)
return denom; 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 /* 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 :) /* if the register was reloaded, then everything should be fine :)
* otherwise, we may need to change some visibility settings. */ * otherwise, we may need to change some visibility settings. */
if (saved) if (saved)
{
sr_set_cell_fractions (reg, new_split);
return; return;
}
/* in the mult-line and dynamic modes, we need to hide the old /* in the mult-line and dynamic modes, we need to hide the old
* and show the new. */ * and show the new. */
@ -794,6 +833,8 @@ LedgerMoveCursor (Table *table, VirtualLocation *p_new_virt_loc)
info->cursor_hint_cursor_class = new_class; info->cursor_hint_cursor_class = new_class;
info->hint_set_by_traverse = FALSE; info->hint_set_by_traverse = FALSE;
sr_set_cell_fractions (reg, new_split);
} }
/* This function determines if auto-completion is appropriate and, /* This function determines if auto-completion is appropriate and,

View File

@ -132,7 +132,7 @@ gnc_locale_default_currency(void)
/* Return the number of decimal places for this locale. */ /* Return the number of decimal places for this locale. */
int int
gnc_locale_decimal_places( void ) gnc_locale_decimal_places (void)
{ {
static gboolean got_it = FALSE; static gboolean got_it = FALSE;
static int places; 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 * static char *
gnc_stpcpy (char *dest, const char *src) gnc_stpcpy (char *dest, const char *src)
{ {
@ -225,21 +189,25 @@ gnc_default_print_info (gboolean use_symbol)
} }
static gboolean 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; return FALSE;
if (max_decimal_places == NULL)
return TRUE;
*max_decimal_places = 0;
while (fraction != 1) while (fraction != 1)
{ {
if (fraction % 10 != 0)
return FALSE;
fraction = fraction / 10; 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; return TRUE;
} }
@ -428,8 +396,7 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info)
} }
/* print the absolute value */ /* print the absolute value */
if (gnc_numeric_negative_p (val)) val = gnc_numeric_abs (val);
val = gnc_numeric_neg (val);
/* calculate the integer part and the remainder */ /* calculate the integer part and the remainder */
whole = gnc_numeric_create (val.num / val.denom, 1); 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); val = gnc_numeric_reduce (val);
printf (temp_buf, " + %lld / %lld", sprintf (temp_buf, " + %lld / %lld",
(long long) val.num, (long long) val.num,
(long long) val.denom); (long long) val.denom);
strcat (buf, temp_buf); strcat (buf, temp_buf);
} }
@ -575,7 +542,7 @@ PrintAmountInternal(char *buf, gnc_numeric val, const GNCPrintAmountInfo *info)
} }
int int
DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info) xaccSPrintAmount (char * bufp, gnc_numeric val, GNCPrintAmountInfo info)
{ {
struct lconv *lc; struct lconv *lc;
@ -589,13 +556,11 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info)
gboolean print_sign = TRUE; gboolean print_sign = TRUE;
if (!bufp) return 0; if (!bufp)
return 0;
lc = gnc_localeconv(); lc = gnc_localeconv();
if (DEQ(val, 0.0))
val = 0.0;
if (info.use_symbol) if (info.use_symbol)
{ {
if (gnc_commodity_equiv (info.commodity, gnc_locale_default_currency ())) if (gnc_commodity_equiv (info.commodity, gnc_locale_default_currency ()))
@ -619,7 +584,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info)
} }
else else
{ {
if (val < 0.0) if (gnc_numeric_negative_p (val))
{ {
cs_precedes = lc->n_cs_precedes; cs_precedes = lc->n_cs_precedes;
sep_by_space = lc->n_sep_by_space; 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 = lc->negative_sign;
sign_posn = lc->n_sign_posn; sign_posn = lc->n_sign_posn;
@ -642,7 +607,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info)
sign_posn = lc->p_sign_posn; 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; print_sign = FALSE;
/* See if we print sign now */ /* See if we print sign now */
@ -673,10 +638,7 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info)
bufp = gnc_stpcpy(bufp, "("); bufp = gnc_stpcpy(bufp, "(");
/* Now print the value */ /* Now print the value */
{ bufp += PrintAmountInternal(bufp, val, &info);
gnc_numeric n = double_to_gnc_numeric (ABS (val), 10000, GNC_RND_ROUND);
bufp += PrintAmountInternal(bufp, n, &info);
}
/* Now see if we print parentheses */ /* Now see if we print parentheses */
if (print_sign && (sign_posn == 0)) if (print_sign && (sign_posn == 0))
@ -710,32 +672,40 @@ DxaccSPrintAmount (char * bufp, double val, GNCPrintAmountInfo info)
} }
const char * const char *
DxaccPrintAmount (double val, GNCPrintAmountInfo info) xaccPrintAmount (gnc_numeric val, GNCPrintAmountInfo info)
{ {
/* hack alert -- this is not thread safe ... */ /* hack alert -- this is not thread safe ... */
static char buf[1024]; static char buf[1024];
DxaccSPrintAmount (buf, val, info); xaccSPrintAmount (buf, val, info);
/* its OK to return buf, since we declared it static */ /* its OK to return buf, since we declared it static */
return buf; return buf;
}
int
xaccSPrintAmount (char * bufp, gnc_numeric val, GNCPrintAmountInfo info)
{
return DxaccSPrintAmount (bufp, gnc_numeric_to_double (val), info);
} }
const char * 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 * * parses amount strings using locale data *
* * * *
* Args: in_str -- pointer to string rep of num * * Args: in_str -- pointer to string rep of num *

View File

@ -343,8 +343,7 @@ gnc_amount_edit_set_amount (GNCAmountEdit *gae, gnc_numeric amount)
gae->amount = amount; gae->amount = amount;
gae->need_to_parse = FALSE; gae->need_to_parse = FALSE;
amount_string = DxaccPrintAmount (gnc_numeric_to_double(amount), amount_string = xaccPrintAmount (amount, gae->print_info);
gae->print_info);
gtk_entry_set_text (GTK_ENTRY (gae->amount_entry), amount_string); gtk_entry_set_text (GTK_ENTRY (gae->amount_entry), amount_string);
} }

View File

@ -1053,7 +1053,7 @@ print_check_cb(GtkWidget * widget, gpointer data)
const char * payee; const char * payee;
const char * memo; const char * memo;
double amount; gnc_numeric amount;
time_t date; time_t date;
SCM print_check = gh_eval_str("gnc:print-check"); 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)) gh_procedure_p(print_check))
{ {
payee = xaccTransGetDescription(trans); payee = xaccTransGetDescription(trans);
amount = DxaccSplitGetValue(split); amount = xaccSplitGetValue(split);
amount = gnc_numeric_abs (amount);
date = xaccTransGetDate(trans); date = xaccTransGetDate(trans);
memo = xaccSplitGetMemo(split); memo = xaccSplitGetMemo(split);
gh_apply(print_check, gh_apply(print_check,
SCM_LIST4(gh_str02scm(payee), SCM_LIST4(gh_str02scm(payee),
gh_double2scm(ABS(amount)), gh_double2scm(gnc_numeric_to_double (amount)),
gh_ulong2scm(date), gh_ulong2scm(date),
gh_str02scm(memo))); gh_str02scm(memo)));
} }

View File

@ -40,6 +40,7 @@
#include "gnc-common.h" #include "gnc-common.h"
#include "gnc-exp-parser.h" #include "gnc-exp-parser.h"
#include "gnc-engine-util.h" #include "gnc-engine-util.h"
#include "gnc-numeric.h"
#include "gnc-ui-util.h" #include "gnc-ui-util.h"
#include "basiccell.h" #include "basiccell.h"

View File

@ -798,6 +798,9 @@ xaccInitSplitRegister (SplitRegister *reg,
/* by default, don't blank zeros on the price cells. */ /* by default, don't blank zeros on the price cells. */
xaccSetPriceCellBlankZero(reg->priceCell, FALSE); 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 /* The reconcile cell should only be entered with the pointer, and
* only then when the user clicks directly on the cell. */ * only then when the user clicks directly on the cell. */
reg->recnCell->cell.input_output |= XACC_CELL_ALLOW_EXACT_ONLY; reg->recnCell->cell.input_output |= XACC_CELL_ALLOW_EXACT_ONLY;