[grisbi-cvs] grisbi/src gsb_currency.c, 1.46, 1.47 gsb_real.c, 1.54, 1.55 gsb_real.h, 1.22, 1.23
Gunee
guneemwelloeux at users.sourceforge.net
Fri Nov 13 20:42:04 CET 2009
Update of /cvsroot/grisbi/grisbi/src
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv28833/src
Modified Files:
gsb_currency.c gsb_real.c gsb_real.h
Log Message:
Partial fix for the problems in currency conversion (in the change dialog box)
Index: gsb_real.c
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/gsb_real.c,v
retrieving revision 1.54
retrieving revision 1.55
diff -u -d -r1.54 -r1.55
--- gsb_real.c 7 Nov 2009 08:09:57 -0000 1.54
+++ gsb_real.c 13 Nov 2009 19:42:02 -0000 1.55
@@ -742,11 +742,31 @@
gsb_real number_2 )
{
gsb_real number;
+ glong reste;
+
+ if ( number_1.mantissa == error_real.mantissa ||
+ number_2.mantissa == error_real.mantissa ||
+ !number_2.mantissa )
+ return error_real;
+
+ reste = number_1.mantissa % number_2.mantissa;
- number = gsb_real_double_to_real ( gsb_real_real_to_double (number_1)
- /
- gsb_real_real_to_double (number_2));
-
+ if( ( number_1.mantissa >= number_2.mantissa ) && !reste )
+ {
+ number.mantissa = number_1.mantissa / number_2.mantissa;
+ number.exponent = number_1.exponent - number_2.exponent;
+ while (number.exponent < 0)
+ {
+ number.mantissa *= 10;
+ number.exponent ++ ;
+ }
+ }
+ else
+ {
+ number = gsb_real_double_to_real_add_exponent (
+ (gdouble) number_1.mantissa / number_2.mantissa,
+ number_1.exponent - number_2.exponent);
+ }
return number;
}
@@ -779,13 +799,21 @@
* */
G_MODULE_EXPORT gsb_real gsb_real_double_to_real ( gdouble number )
{
- gdouble tmp_double;
+ return gsb_real_double_to_real_add_exponent(number, 0);
+}
+
+gsb_real gsb_real_double_to_real_add_exponent ( gdouble number, gint exp_add )
+{
+ gdouble tmp_double, decimal;
gdouble maxlong;
gsb_real real_number = null_real;
maxlong = G_MAXLONG / 10;
- while (modf (number, &tmp_double))
+ if(exp_add >=9)
+ return null_real;
+
+ while (modf (number, &tmp_double) && real_number.exponent < (9-exp_add) )
{
number = number * 10;
real_number.exponent++;
@@ -793,7 +821,12 @@
if (fabs (number) > maxlong)
number = rint (number);
}
- real_number.mantissa = number;
+ decimal = modf ( number, &tmp_double );
+ if ( ( (real_number.exponent == (9-exp_add)) ) && (decimal >= 0.5) )
+ real_number.mantissa = ((glong) number ) + 1;
+
+ real_number.mantissa = (glong) (number);
+ real_number.exponent += exp_add;
return real_number;
}
@@ -844,6 +877,71 @@
return number_tmp;
}
+
+/* Function to transform string into gsb_real */
+gsb_real gsb_str_to_real ( const gchar * str )
+{
+ gchar **numb, **ff, **ss, *err;
+ gchar *sss, *ttt, *f, *s, *a, *b, *new_str;
+ int decimals;
+ glong nombre;
+ gsb_real resu;
+ gsb_real null_real = { 0 , 0 };
+ gsb_real error_real = { 0x80000000, 0 };
+
+ //new_str = g_convert ( str, -1, "ASCII", "UTF8", NULL, NULL, NULL);
+
+ if(!str || !g_utf8_strlen( str , -1 ))
+ return null_real;
+ numb = g_strsplit_set(str, ",.",2);
+ if( !numb[0] || !g_utf8_strlen( numb[0] , -1 ))
+ a = g_strdup ( "0" );
+ else
+ a = g_strdup(numb[0]);
+ if( !numb[1] || !g_utf8_strlen( numb[1], -1 ))
+ b = g_strdup ( "0" );
+ else
+ b = g_strdup(numb[1]);
+ g_strfreev(numb);
+ ff = g_strsplit ( a , " ", -1);
+ ss = g_strsplit ( b , " ", -1);
+ g_free(a);
+ g_free(b);
+ f = g_strjoinv ("", ff);
+ s = g_strjoinv ("", ss);
+ g_strfreev ( ff );
+ g_strfreev ( ss );
+ decimals = strlen(s);
+ sss = g_strconcat(f, s, NULL);
+ puts(sss);
+ ttt = g_convert(sss, -1, "ASCII", "UTF8", NULL, NULL, NULL);
+ puts(ttt);
+ g_free(f);
+ g_free(s);
+ errno = 0;
+ nombre = strtol(ttt, &err, 10);
+ if(errno == ERANGE)
+ {
+ g_free(err);
+ g_free(sss);
+ return error_real;
+ }
+ g_free(sss);
+ if( *err != '\0' ){
+ puts(err);
+ return error_real;
+ }
+ if( !nombre )
+ return null_real;
+ while( (! (nombre % 10)) ){
+ nombre /= 10;
+ decimals --;
+ }
+ resu.mantissa = nombre;
+ resu.exponent = decimals;
+ return resu;
+}
+
/* Local Variables: */
/* c-basic-offset: 4 */
/* End: */
Index: gsb_real.h
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/gsb_real.h,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- gsb_real.h 7 Nov 2009 08:09:57 -0000 1.22
+++ gsb_real.h 13 Nov 2009 19:42:02 -0000 1.23
@@ -16,6 +16,7 @@
/* END_INCLUDE_H */
/* START_DECLARATION */
+gsb_real gsb_str_to_real ( const gchar * str );
gsb_real gsb_real_abs ( gsb_real number );
G_MODULE_EXPORT gsb_real gsb_real_add ( gsb_real number_1,
gsb_real number_2 );
@@ -26,6 +27,7 @@
gsb_real gsb_real_div ( gsb_real number_1,
gsb_real number_2 );
G_MODULE_EXPORT gsb_real gsb_real_double_to_real ( gdouble number );
+gsb_real gsb_real_double_to_real_add_exponent ( gdouble number, gint exp_add );
gsb_real gsb_real_get_from_string ( const gchar *string );
gchar *gsb_real_get_string ( gsb_real number );
gchar *gsb_real_get_string_with_currency ( gsb_real number,
Index: gsb_currency.c
===================================================================
RCS file: /cvsroot/grisbi/grisbi/src/gsb_currency.c,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -d -r1.46 -r1.47
--- gsb_currency.c 9 Nov 2009 14:07:05 -0000 1.46
+++ gsb_currency.c 13 Nov 2009 19:42:02 -0000 1.47
@@ -783,10 +783,8 @@
else
{
gtk_widget_set_sensitive ( GTK_WIDGET ( entry ), FALSE );
- amount_1 = gsb_real_get_from_string (
- gtk_entry_get_text ( GTK_ENTRY ( entry_1 ) ) );
- amount_2 = gsb_real_get_from_string (
- gtk_entry_get_text ( GTK_ENTRY ( entry_2 ) ) );
+ amount_1 = gsb_str_to_real ( gtk_entry_get_text ( GTK_ENTRY ( entry_1 ) ) );
+ amount_2 = gsb_str_to_real ( gtk_entry_get_text ( GTK_ENTRY ( entry_2 ) ) );
taux = gsb_real_div ( amount_2, amount_1 );
gtk_entry_set_text ( GTK_ENTRY ( entry ), gsb_real_get_string ( taux ) );
}
More information about the cvs
mailing list