aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy
diff options
context:
space:
mode:
authorDanielle Madeley <danielle.madeley@collabora.co.uk>2011-04-21 11:13:10 +0800
committerDanielle Madeley <danielle.madeley@collabora.co.uk>2011-04-21 11:13:10 +0800
commit93f10e9c7dad9be29c2f6248058cdd61123ed9d6 (patch)
treec0e43bbb19a6b88d675f39628666cb0967f67e41 /libempathy
parent462c1b74848498594239e818270efbcc0cac5170 (diff)
parentce0ef4346f2042295b18553ae47280adc708d169 (diff)
downloadgsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.tar
gsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.tar.gz
gsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.tar.bz2
gsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.tar.lz
gsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.tar.xz
gsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.tar.zst
gsoc2013-empathy-93f10e9c7dad9be29c2f6248058cdd61123ed9d6.zip
Merge branch 'balance-31-2' into empathy-skype
Diffstat (limited to 'libempathy')
-rw-r--r--libempathy/empathy-utils.c75
-rw-r--r--libempathy/empathy-utils.h2
2 files changed, 77 insertions, 0 deletions
diff --git a/libempathy/empathy-utils.c b/libempathy/empathy-utils.c
index 24d7b544b..8d4cd8d68 100644
--- a/libempathy/empathy-utils.c
+++ b/libempathy/empathy-utils.c
@@ -29,6 +29,7 @@
#include "config.h"
#include <string.h>
+#include <math.h>
#include <time.h>
#include <sys/types.h>
@@ -796,3 +797,77 @@ empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert)
return NULL;
}
+
+gchar *
+empathy_format_currency (gint amount,
+ guint scale,
+ const gchar *currency)
+{
+#define MINUS "\342\210\222"
+#define EURO "\342\202\254"
+#define YEN "\302\245"
+#define POUND "\302\243"
+
+ /* localised representations of currency */
+ /* FIXME: check these, especially negatives and decimals */
+ static const struct {
+ const char *currency;
+ const char *positive;
+ const char *negative;
+ const char *decimal;
+ } currencies[] = {
+ /* sym positive negative decimal */
+ { "EUR", EURO "%s", MINUS EURO "%s", "." },
+ { "USD", "$%s", MINUS "$%s", "." },
+ { "JPY", YEN "%s" MINUS YEN "%s", "." },
+ { "GBP", POUND "%s", MINUS POUND "%s", "." },
+ { "PLN", "%s zl", MINUS "%s zl", "." },
+ { "BRL", "R$%s", MINUS "R$%s", "." },
+ { "SEK", "%s kr", MINUS "%s kr", "." },
+ { "DKK", "kr %s", "kr " MINUS "%s", "." },
+ { "HKD", "$%s", MINUS "$%s", "." },
+ { "CHF", "%s Fr.", MINUS "%s Fr.", "." },
+ { "NOK", "kr %s", "kr" MINUS "%s", "," },
+ { "CAD", "$%s", MINUS "$%s", "." },
+ { "TWD", "$%s", MINUS "$%s", "." },
+ { "AUD", "$%s", MINUS "$%s", "." },
+ };
+
+ const char *positive = "%s";
+ const char *negative = MINUS "%s";
+ const char *decimal = ".";
+ char *fmt_amount, *money;
+ guint i;
+
+ /* get the localised currency format */
+ for (i = 0; i < G_N_ELEMENTS (currencies); i++) {
+ if (!tp_strdiff (currency, currencies[i].currency)) {
+ positive = currencies[i].positive;
+ negative = currencies[i].negative;
+ decimal = currencies[i].decimal;
+ break;
+ }
+ }
+
+ /* format the amount using the scale */
+ if (scale == 0) {
+ /* no decimal point required */
+ fmt_amount = g_strdup_printf ("%d", amount);
+ } else {
+ /* don't use floating point arithmatic, it's noisy;
+ * we take the absolute values, because we want the minus
+ * sign to appear before the $ */
+ int divisor = pow (10, scale);
+ int dollars = abs (amount / divisor);
+ int cents = abs (amount % divisor);
+
+ fmt_amount = g_strdup_printf ("%d%s%0*d",
+ dollars, decimal, scale, cents);
+ }
+
+ money = g_strdup_printf (amount < 0 ? negative : positive, fmt_amount);
+ g_free (fmt_amount);
+
+ return money;
+}
+
diff --git a/libempathy/empathy-utils.h b/libempathy/empathy-utils.h
index 2ddfb4c53..7ec96be13 100644
--- a/libempathy/empathy-utils.h
+++ b/libempathy/empathy-utils.h
@@ -102,6 +102,8 @@ gboolean empathy_folks_persona_is_interesting (FolksPersona *persona);
gchar * empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert);
+gchar *empathy_format_currency (gint amount, guint scale, const gchar *currency);
+
G_END_DECLS
#endif /* __EMPATHY_UTILS_H__ */