[gnome-software/wip/rancell/paid] Store price as a double - need cents



commit d5334d7e980628ef02e65b10200b015a8b01e5ef
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu May 5 17:10:58 2016 +1200

    Store price as a double - need cents

 src/gs-app.c   |    8 ++++----
 src/gs-app.h   |    2 +-
 src/gs-price.c |   42 ++++++++++++++++++++++--------------------
 src/gs-price.h |    8 ++++----
 4 files changed, 31 insertions(+), 29 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index ae591ac..40d1e96 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -310,10 +310,10 @@ gs_app_to_string (GsApp *app)
                gs_app_kv_lpad (str, "origin-ui", app->origin_ui);
        for (i = 0; i < app->prices->len; i++) {
                GsPrice *price = g_ptr_array_index (app->prices, i);
-               g_autofree gchar *key = NULL;
+               g_autofree gchar *key = NULL, *text = NULL;
                key = g_strdup_printf ("price-%02i", i);
-               gs_app_kv_printf (str, key, "%u %s",
-                                 gs_price_get_amount (price), gs_price_get_currency (price));
+               text = gs_price_to_string (price);
+               gs_app_kv_lpad (str, key, text);
        }
        if (app->rating != -1)
                gs_app_kv_printf (str, "rating", "%i", app->rating);
@@ -1775,7 +1775,7 @@ gs_app_get_prices (GsApp *app)
  * gs_app_add_price:
  */
 void
-gs_app_add_price (GsApp *app, guint amount, const gchar *currency)
+gs_app_add_price (GsApp *app, gdouble amount, const gchar *currency)
 {
        g_return_if_fail (GS_IS_APP (app));
        g_ptr_array_add (app->prices, gs_price_new (amount, currency));
diff --git a/src/gs-app.h b/src/gs-app.h
index 9ea71ac..cd40600 100644
--- a/src/gs-app.h
+++ b/src/gs-app.h
@@ -198,7 +198,7 @@ void                 gs_app_set_metadata            (GsApp          *app,
                                                 const gchar    *value);
 GPtrArray      *gs_app_get_prices              (GsApp          *app);
 void            gs_app_add_price               (GsApp          *app,
-                                                guint           amount,
+                                                gdouble         amount,
                                                 const gchar    *currency);
 gboolean        gs_app_get_owned               (GsApp          *app);
 void            gs_app_set_owned               (GsApp          *app,
diff --git a/src/gs-price.c b/src/gs-price.c
index f178b88..5cd5402 100644
--- a/src/gs-price.c
+++ b/src/gs-price.c
@@ -29,7 +29,7 @@ struct _GsPrice
 {
        GObject                  parent_instance;
 
-       gint                     amount;
+       gdouble                  amount;
        gchar                   *currency;
 };
 
@@ -45,7 +45,7 @@ G_DEFINE_TYPE (GsPrice, gs_price, G_TYPE_OBJECT)
 /**
  * gs_price_get_amount:
  */
-guint
+gdouble
 gs_price_get_amount (GsPrice *price)
 {
        g_return_val_if_fail (GS_IS_PRICE (price), 0);
@@ -56,7 +56,7 @@ gs_price_get_amount (GsPrice *price)
  * gs_price_set_amount:
  */
 void
-gs_price_set_amount (GsPrice *price, guint amount)
+gs_price_set_amount (GsPrice *price, gdouble amount)
 {
        g_return_if_fail (GS_IS_PRICE (price));
        price->amount = amount;
@@ -92,25 +92,25 @@ gs_price_to_string (GsPrice *price)
        g_return_val_if_fail (GS_IS_PRICE (price), NULL);
 
        if (g_strcmp0 (price->currency, "AUD") == 0) {
-               return g_strdup_printf (_("A$%u"), price->amount);
+               return g_strdup_printf (_("A$%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "CAD") == 0) {
-               return g_strdup_printf (_("C$%u"), price->amount);
+               return g_strdup_printf (_("C$%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "CNY") == 0) {
-               return g_strdup_printf (_("CN¥%u"), price->amount);
+               return g_strdup_printf (_("CN¥%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "EUR") == 0) {
-               return g_strdup_printf (_("€%u"), price->amount);
+               return g_strdup_printf (_("€%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "GBP") == 0) {
-               return g_strdup_printf (_("£%u"), price->amount);
+               return g_strdup_printf (_("£%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "JPY") == 0) {
-               return g_strdup_printf (_("¥%u"), price->amount);
+               return g_strdup_printf (_("¥%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "NZD") == 0) {
-               return g_strdup_printf (_("NZ$%u"), price->amount);
+               return g_strdup_printf (_("NZ$%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "RUB") == 0) {
-               return g_strdup_printf (_("₽%u"), price->amount);
+               return g_strdup_printf (_("₽%.2f"), price->amount);
        } else if (g_strcmp0 (price->currency, "USD") == 0) {
-               return g_strdup_printf (_("US$%u"), price->amount);
+               return g_strdup_printf (_("US$%.2f"), price->amount);
        } else {
-               return g_strdup_printf (_("%s %u"), price->currency, price->amount);
+               return g_strdup_printf (_("%s %f"), price->currency, price->amount);
        }
 }
 
@@ -122,7 +122,7 @@ gs_price_get_property (GObject *object, guint prop_id,
 
        switch (prop_id) {
        case PROP_AMOUNT:
-               g_value_set_uint (value, price->amount);
+               g_value_set_double (value, price->amount);
                break;
        case PROP_CURRENCY:
                g_value_set_string (value, price->currency);
@@ -141,7 +141,7 @@ gs_price_set_property (GObject *object, guint prop_id,
 
        switch (prop_id) {
        case PROP_AMOUNT:
-               gs_price_set_amount (price, g_value_get_uint (value));
+               gs_price_set_amount (price, g_value_get_double (value));
                break;
        case PROP_CURRENCY:
                gs_price_set_currency (price, g_value_get_string (value));
@@ -174,9 +174,9 @@ gs_price_class_init (GsPriceClass *klass)
        /**
         * GsApp:amount:
         */
-       pspec = g_param_spec_uint ("amount", NULL, NULL,
-                                 0, G_MAXUINT, 0,
-                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+       pspec = g_param_spec_double ("amount", NULL, NULL,
+                                    G_MINDOUBLE, G_MAXDOUBLE, 0.0,
+                                    G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
        g_object_class_install_property (object_class, PROP_AMOUNT, pspec);
 
        /**
@@ -199,10 +199,12 @@ gs_price_init (GsPrice *price)
  * Return value: a new #GsPrice object.
  **/
 GsPrice *
-gs_price_new (guint amount, const gchar *currency)
+gs_price_new (gdouble amount, const gchar *currency)
 {
        GsPrice *price;
-       price = g_object_new (GS_TYPE_PRICE, "amount", amount, "currency", currency, NULL);
+       price = g_object_new (GS_TYPE_PRICE, NULL);
+       gs_price_set_amount (price, amount);
+       gs_price_set_currency (price, currency);
        return GS_PRICE (price);
 }
 
diff --git a/src/gs-price.h b/src/gs-price.h
index a43a9cd..aab8677 100644
--- a/src/gs-price.h
+++ b/src/gs-price.h
@@ -30,12 +30,12 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GsPrice, gs_price, GS, PRICE, GObject)
 
-GsPrice                *gs_price_new                           (guint amount,
-                                                        const gchar *currency);
+GsPrice                *gs_price_new                           (gdouble         amount,
+                                                        const gchar    *currency);
 
-guint           gs_price_get_amount                    (GsPrice        *price);
+gdouble                 gs_price_get_amount                    (GsPrice        *price);
 void            gs_price_set_amount                    (GsPrice        *price,
-                                                        guint           amount);
+                                                        gdouble         amount);
 
 const gchar    *gs_price_get_currency                  (GsPrice        *price);
 void            gs_price_set_currency                  (GsPrice        *price,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]