Format numbers with commas
authorRyan Campbell <campbellr@gmail.com>
Sat, 22 May 2010 04:24:36 +0000 (22:24 -0600)
committerRyan Campbell <campbellr@gmail.com>
Sat, 22 May 2010 04:24:36 +0000 (22:24 -0600)
created a new function comma() that converts a number into a string
formatted with commas.
eg. 1234567 -> "1,234,567"

Also, put this in new file util.py, which can hold miscellaneous functions.

package/src/ui/diablo/gui.py
package/src/ui/fremantle/gui.py
package/src/ui/models.py
package/src/util.py [new file with mode: 0644]

index 2fe9e56..a0b2131 100644 (file)
@@ -26,6 +26,7 @@ import glib
 
 from ui import models
 import validation
+import util
 
 class BaseUI():
     menu_items = ("Settings", "About", "Refresh")
@@ -412,11 +413,12 @@ class CharacterSheetUI(BaseUI):
             self.sheet.race, self.sheet.bloodLine), box)
         self.add_label("", box, markup=False)
         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
-        self.add_label("<small><b>Balance:</b> %s ISK</small>" % self.sheet.balance, box)
+        self.add_label("<small><b>Balance:</b> %s ISK</small>" % 
+                util.comma(self.sheet.balance), box)
 
         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
-        self.live_sp = self.add_label("<small><b>Total SP:</b> %d</small>" %
-                self.live_sp_val, box)
+        self.live_sp = self.add_label("<small><b>Total SP:</b> %s</small>" %
+                util.comma(int(self.live_sp_val)), box)
         
         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
 
@@ -475,8 +477,8 @@ class CharacterSheetUI(BaseUI):
         #    return False
         
         self.live_sp_val = self.live_sp_val + self.spps * self.UPDATE_INTERVAL
-        self.live_sp.set_label("<small><b>Total SP:</b> %d</small>" %
-                                self.live_sp_val)
+        self.live_sp.set_label("<small><b>Total SP:</b> %s</small>" %
+                                util.comma(int(self.live_sp_val)))
 
         return True
 
index c66032a..ed15197 100644 (file)
@@ -26,6 +26,7 @@ import glib
 
 from ui import models
 import validation
+import util
 
 class BaseUI():
     menu_items = ("Settings", "About", "Refresh")
@@ -414,11 +415,12 @@ class CharacterSheetUI(BaseUI):
             self.sheet.race, self.sheet.bloodLine), box)
         self.add_label("", box, markup=False)
         self.add_label("<small><b>Corp:</b> %s</small>" % self.sheet.corporationName, box)
-        self.add_label("<small><b>Balance:</b> %s ISK</small>" % self.sheet.balance, box)
+        self.add_label("<small><b>Balance:</b> %s ISK</small>" % 
+                util.comma(self.sheet.balance), box)
 
         self.live_sp_val = self.controller.get_sp(self.uid, self.char_id)
-        self.live_sp = self.add_label("<small><b>Total SP:</b> %d</small>" %
-                self.live_sp_val, box)
+        self.live_sp = self.add_label("<small><b>Total SP:</b> %s</small>" %
+                util.comma(int(self.live_sp_val)), box)
         
         self.spps = self.controller.get_spps(self.uid, self.char_id)[0]
 
@@ -476,8 +478,8 @@ class CharacterSheetUI(BaseUI):
         #    return False
         
         self.live_sp_val = self.live_sp_val + self.spps * self.UPDATE_INTERVAL
-        self.live_sp.set_label("<small><b>Total SP:</b> %d</small>" %
-                                self.live_sp_val)
+        self.live_sp.set_label("<small><b>Total SP:</b> %s</small>" %
+                                util.comma(int(self.live_sp_val)))
 
         return True
 
index c83415d..24487bb 100644 (file)
@@ -1,4 +1,5 @@
 import gtk
+import util
 
 class AccountsModel(gtk.ListStore):
     C_UID, C_APIKEY, C_CHARS = range(3)
@@ -91,6 +92,6 @@ class CharacterSkillsModel(gtk.ListStore):
                     liter = self.append()
                     self.set(liter, self.C_NAME, "%s" % skill.typeName,
                                       self.C_RANK, "<small>(Rank %d)</small>" % skill.rank,
-                                      self.C_SKILLPOINTS, "SP: %d" % trained.skillpoints,
+                                      self.C_SKILLPOINTS, "SP: %s" % util.comma(trained.skillpoints),
                                       self.C_LEVEL, "Level %d" % trained.level)
 
diff --git a/package/src/util.py b/package/src/util.py
new file mode 100644 (file)
index 0000000..f2c7955
--- /dev/null
@@ -0,0 +1,21 @@
+#Random helpful functions for mevemon
+def comma(d):
+    """
+    Converts a number in the format 1234567 to 1,234,567
+    """
+    s = '%0.2f' % d
+    
+    a,b = s.split('.')
+    l = []
+    while len(a) > 3:
+        l.insert(0,a[-3:])
+        a = a[0:-3]
+    if a:
+        l.insert(0,a)
+
+    if type(d) is int:
+        return ','.join(l)
+    else:
+        return ','.join(l)+'.'+b
+