From: Andrea Zagli Date: Tue, 15 Feb 2011 08:20:16 +0000 (+0100) Subject: Aggiunta funzione Solipa::round. X-Git-Tag: Pre_EvolutionDataServer_2.32~4 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=5de90d00db1094a4e6b6fd7840f10fa0e9c43ad6;p=solipa%2Flibsolipa Aggiunta funzione Solipa::round. --- diff --git a/src/utils.c b/src/utils.c index 6d27d44..cdfb92e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -21,6 +21,7 @@ #endif #include +#include #include #include @@ -567,6 +568,23 @@ GDate (GDateYear)tt_tm->tm_year + 1900); } +gdouble +solipa_round (gdouble value, guint n_decimals) +{ + gfloat elev; + gint i; + gfloat ret; + + elev = pow (10.0, (gfloat)n_decimals); + + ret = (value * elev) + 0.5; + i = (gint)ret; + + ret = (gfloat)i / elev; + + return ret; +} + /** * This function is copied from * http://bugzilla.gnome.org/show_bug.cgi?id=524831. diff --git a/src/utils.h b/src/utils.h index 6080ae0..3062590 100644 --- a/src/utils.h +++ b/src/utils.h @@ -49,6 +49,8 @@ gchar *solipa_format_money (gdouble number, gint decimals, gboolean with_currenc struct tm *solipa_get_now_tm (void); GDate *solipa_get_today_gdate (void); +gdouble solipa_round (gdouble value, guint n_decimals); + gchar *g_mkdtemp (gchar *tmpl); diff --git a/tests/Makefile.am b/tests/Makefile.am index 4b9b506..457715f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -12,4 +12,5 @@ LDADD = $(top_builddir)/src/libsolipa.la noinst_PROGRAMS = allegato \ mail \ utils \ - utils_format_money + utils_format_money \ + utils_round diff --git a/tests/utils_round.c b/tests/utils_round.c new file mode 100644 index 0000000..2ca7a09 --- /dev/null +++ b/tests/utils_round.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2010-2011 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +int +main (int argc, char *argv[]) +{ + gtk_init (&argc, &argv); + + g_message ("%f", solipa_round (g_strtod (argv[1], NULL), 2)); + + return 0; +}