From d5ca54a2f614baff95c0b6c485352af4dd0fcbc1 Mon Sep 17 00:00:00 2001 From: Scott Torborg Date: Mon, 2 Nov 2009 13:19:51 -1000 Subject: [PATCH] use python's locale stuff for currency formatting --- bluechips/model/types.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/bluechips/model/types.py b/bluechips/model/types.py index d445481..673a366 100644 --- a/bluechips/model/types.py +++ b/bluechips/model/types.py @@ -2,11 +2,36 @@ Define special types used in BlueChips """ +import locale + import sqlalchemy as sa from bluechips.lib.subclass import SmartSubclass from weakref import WeakValueDictionary +def localeconv(): + "Manually install en_US for systems that don't have it." + d = {'currency_symbol': '$', + 'decimal_point': '.', + 'frac_digits': 2, + 'grouping': [3, 3, 0], + 'int_curr_symbol': 'USD ', + 'int_frac_digits': 2, + 'mon_decimal_point': '.', + 'mon_grouping': [3, 3, 0], + 'mon_thousands_sep': ',', + 'n_cs_precedes': 1, + 'n_sep_by_space': 0, + 'n_sign_posn': 1, + 'negative_sign': '-', + 'p_cs_precedes': 1, + 'p_sep_by_space': 0, + 'p_sign_posn': 1, + 'positive_sign': '', + 'thousands_sep': ','} + return d +locale.localeconv = localeconv + class Currency(object): """ Store currency values as an integral number of cents @@ -80,10 +105,7 @@ class Currency(object): def __repr__(self): return '%s("%s")' % (self.__class__.__name__, str(self)) def __str__(self): - sign = '-' if self.value < 0 else '' - cents = abs(self.value) % 100 - dollars = (abs(self.value) - cents) / 100 - return '%s$%s.%.02d' % (sign, dollars, cents) + return locale.currency(self.value / 100., grouping=True) class DBCurrency(sa.types.TypeDecorator): """ -- 2.45.2