* src/gnome-util/dialog-transfer.c: fix a bunch more rounding

errors, and add more precision to the price.  There is still a
	  problem when acquiring a high-precision price out of the pricedb
	  where it will get truncated in the AmountEdit widget.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@8880 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Derek Atkins 2003-07-15 01:52:07 +00:00
parent 300c1af7ab
commit 386e8175f2
2 changed files with 32 additions and 21 deletions

View File

@ -6,6 +6,11 @@
* configure.in: search for db-4.1. Fixes #116509.
* src/gnome-util/dialog-transfer.c: fix a bunch more rounding
errors, and add more precision to the price. There is still a
problem when acquiring a high-precision price out of the pricedb
where it will get truncated in the AmountEdit widget.
2003-07-10 Derek Atkins <derek@ihtfp.com>
* src/import-export/import-parse.[ch]: routines to parse numbers

View File

@ -47,7 +47,7 @@
#define DIALOG_TRANSFER_CM_CLASS "dialog-transfer"
#define PRECISION 100000
#define PRECISION 1000000
typedef enum
{
XFER_DIALOG_FROM,
@ -140,6 +140,17 @@ static void gnc_xfer_dialog_update_conv_info(XferDialog *xferData);
/** Implementations **********************************************/
static gnc_numeric
gnc_xfer_dialog_compute_price (XferDialog *xferData)
{
gnc_numeric from_amt, to_amt;
from_amt = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->amount_edit));
to_amt = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->to_amount_edit));
return(gnc_numeric_div(to_amt, from_amt, GNC_DENOM_AUTO, GNC_DENOM_REDUCE));
}
/* (maybe) update the price from the pricedb. */
static void
gnc_xfer_dialog_update_price (XferDialog *xferData)
@ -235,8 +246,7 @@ gnc_xfer_dialog_set_price_auto (XferDialog *xferData,
if (gnc_numeric_zero_p (from_rate) || gnc_numeric_zero_p (to_rate))
gnc_xfer_dialog_update_price (xferData);
price = gnc_numeric_div (to_rate, from_rate, GNC_DENOM_AUTO,
GNC_DENOM_SIGFIGS(6) | GNC_RND_ROUND);
price = gnc_numeric_div (to_rate, from_rate, GNC_DENOM_AUTO, GNC_DENOM_REDUCE);
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(xferData->price_edit), price);
@ -828,8 +838,10 @@ gnc_xfer_dialog_update_conv_info (XferDialog *xferData)
from_mnemonic = gnc_commodity_get_mnemonic(xferData->from_commodity);
to_mnemonic = gnc_commodity_get_mnemonic(xferData->to_commodity);
price = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));
if (gnc_numeric_zero_p(price)) {
// price = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));
price = gnc_xfer_dialog_compute_price(xferData);
if (gnc_numeric_check(price) || gnc_numeric_zero_p(price)) {
string = g_strdup_printf("1 %s = x %s", from_mnemonic, to_mnemonic);
gtk_label_set_text(GTK_LABEL(xferData->conv_forward), string);
g_free(string);
@ -942,7 +954,7 @@ gnc_xfer_to_amount_update_cb(GtkWidget *widget, GdkEventFocus *event,
gpointer data)
{
XferDialog *xferData = data;
gnc_numeric amount, price, to_amount;
gnc_numeric price;
Account *account;
account = gnc_account_tree_get_current_account(xferData->to);
@ -951,10 +963,8 @@ gnc_xfer_to_amount_update_cb(GtkWidget *widget, GdkEventFocus *event,
gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (xferData->to_amount_edit));
amount = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->amount_edit));
to_amount = gnc_amount_edit_get_amount
(GNC_AMOUNT_EDIT(xferData->to_amount_edit));
price = gnc_numeric_div (to_amount, amount, PRECISION, GNC_RND_ROUND);
price = gnc_xfer_dialog_compute_price(xferData);
price = gnc_numeric_convert (price, PRECISION, GNC_RND_ROUND);
gnc_amount_edit_set_amount(GNC_AMOUNT_EDIT(xferData->price_edit), price);
gnc_xfer_dialog_update_conv_info(xferData);
@ -1423,7 +1433,7 @@ gnc_xfer_dialog_ok_cb(GtkWidget * widget, gpointer data)
if (xferData->exch_rate)
{
gnc_numeric to_amt, from_amt;
gnc_numeric price;
/* If we've got the price-button set, then make sure we update the
* to-amount before we use it.
@ -1431,12 +1441,8 @@ gnc_xfer_dialog_ok_cb(GtkWidget * widget, gpointer data)
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(xferData->price_radio)))
gnc_xfer_update_to_amount(xferData);
from_amt = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT(xferData->amount_edit));
to_amt = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT(xferData->to_amount_edit));
*(xferData->exch_rate) =
gnc_numeric_abs (gnc_numeric_div (to_amt, from_amt, GNC_DENOM_AUTO,
GNC_DENOM_REDUCE));
price = gnc_xfer_dialog_compute_price(xferData);
*(xferData->exch_rate) = gnc_numeric_abs(price);
}
else
{
@ -1523,20 +1529,20 @@ gnc_xfer_dialog_ok_cb(GtkWidget * widget, gpointer data)
gnc_numeric value;
/* compute the price -- maybe we need to swap? */
value = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));
value = gnc_xfer_dialog_compute_price(xferData);
value = gnc_numeric_abs (value);
/* Try to be consistent about how quotes are installed. */
if (from == gnc_default_currency()) {
tmp = from; from = to; to = tmp;
value = gnc_numeric_div (gnc_numeric_create(1,1), value,
PRECISION, GNC_RND_ROUND);
GNC_DENOM_AUTO, GNC_DENOM_REDUCE);
} else if ((to != gnc_default_currency()) &&
(strcmp (gnc_commodity_get_mnemonic(from),
gnc_commodity_get_mnemonic(to)) < 0)) {
tmp = from; from = to; to = tmp;
value = gnc_numeric_div (gnc_numeric_create(1,1), value,
PRECISION, GNC_RND_ROUND);
GNC_DENOM_AUTO, GNC_DENOM_REDUCE);
}
price = gnc_price_create (xferData->book);
@ -1743,7 +1749,7 @@ gnc_xfer_dialog_create(GtkWidget * parent, XferDialog *xferData)
edit = gnc_amount_edit_new();
gnc_amount_edit_set_print_info(GNC_AMOUNT_EDIT(edit),
gnc_default_price_print_info ());
gnc_default_print_info (FALSE));
gnc_amount_edit_set_fraction(GNC_AMOUNT_EDIT(edit), PRECISION);
hbox = glade_xml_get_widget (xml, "price_hbox");
gtk_box_pack_start(GTK_BOX(hbox), edit, TRUE, TRUE, 0);