Repository: gratipay/gratipay.com

Issue:
dollar amounts no longer showing two decimals
Presumably a regression from recent i18n work.

![screen shot 2014-08-25 at 1 40 06 pm](https://cloud.githubusercontent.com/assets/134455/4033748/f1229a02-2c7e-11e4-92cf-5cc3459506f3.png)



Discussion:
Goofier example:

![screen shot 2014-08-25 at 1 42 44 pm](https://cloud.githubusercontent.com/assets/134455/4033787/46e555b0-2c7f-11e4-8a6c-29d471e9a77c.png)

I removed the trailing zeros because they looked bad on the buttons of the "support Gittip" banner. We could support both formats by adding an argument like `trailing_zeros=False` to the `format_currency` function.

> I removed the trailing zeros because they looked bad on the buttons of the "support Gittip" banner.

What are you referencing? There aren't any dollar values on the banner:
![screen shot 2014-08-25 at 12 58 05 pm](https://cloud.githubusercontent.com/assets/688886/4033988/6df19ac2-2c81-11e4-9992-ee6d1e1bf0f2.png)

@seanlinsley Aren't there? What about the button text?

@Changaco We could also go back to hard-wiring the button text. What do different locales even do in this case? `1$` instead of `$1`? Something like that?

@whit537 Yes, the currency symbol can be after, it can be different than `$` (e.g. `US$`), and in some locales (e.g. French) there is a non-breaking space between the number and the symbol. So I'm in favor of fixing this properly, not going back to hard-wired English.

Another:

![screen shot 2014-08-26 at 9 53 06 am](https://cloud.githubusercontent.com/assets/134455/4045281/5f3a0486-2d28-11e4-9517-3ff0f14a9de2.png)


Resolving patch:
diff --git a/gratipay/utils/i18n.py b/gratipay/utils/i18n.py
--- a/gratipay/utils/i18n.py
+++ b/gratipay/utils/i18n.py
@@ -109,8 +109,7 @@ def load_langs(localeDir):
 LOCALE_EN.countries_map = COUNTRIES_MAP
 
 # Patch the locales to look less formal
-LOCALE_EN.currency_formats[None] = parse_pattern('\xa4#,##0.##')
-LOCALES['fr'].currency_formats[None] = parse_pattern('#,##0.##\u202f\xa4')
+LOCALES['fr'].currency_formats[None] = parse_pattern('#,##0.00\u202f\xa4')
 LOCALES['fr'].currency_symbols['USD'] = '$'
 
 
@@ -125,6 +124,13 @@ def get_locale_for_request(request):
     return LOCALE_EN
 
 
+def format_currency_with_options(number, currency, locale=LOCALE_EN, trailing_zeroes=True):
+    s = format_currency(number, currency, locale=locale)
+    if not trailing_zeroes:
+        s = s.replace(get_decimal_symbol(locale)+'00', '')
+    return s
+
+
 def inbound(request):
     context = request.context
     loc = context.locale = get_locale_for_request(request)
@@ -133,7 +139,7 @@ def inbound(request):
     context.ngettext = lambda *a, **kw: n_get_text(request, loc, *a, **kw)
     context.format_number = lambda *a: format_number(*a, locale=loc)
     context.format_decimal = lambda *a: format_decimal(*a, locale=loc)
-    context.format_currency = lambda *a: format_currency(*a, locale=loc)
+    context.format_currency = lambda *a, **kw: format_currency_with_options(*a, locale=loc, **kw)
     context.format_percent = lambda *a: format_percent(*a, locale=loc)
     context.parse_decimal = lambda *a: parse_decimal(*a, locale=loc)
     def _to_age(delta):


Test patch:
diff --git a/tests/py/test_utils.py b/tests/py/test_utils.py
--- a/tests/py/test_utils.py
+++ b/tests/py/test_utils.py
@@ -9,6 +9,7 @@
 from gratipay.testing import Harness
 from gratipay.utils.username import safely_reserve_a_username, FailedToReserveUsername, \
                                                                            RanOutOfUsernameAttempts
+from gratipay.utils import i18n
 from psycopg2 import IntegrityError
 
 
@@ -76,6 +77,21 @@ def test_long_difference_not_expiring(self):
         expiring = utils.is_card_expiring(expiring.year, expiring.month)
         assert not expiring
 
+    def test_format_currency_without_trailing_zeroes(self):
+        expected = '$16'
+        actual = i18n.format_currency_with_options(16, 'USD', locale=i18n.Locale('en'), trailing_zeroes=False)
+        assert actual == expected
+
+    def test_format_currency_with_trailing_zeroes(self):
+        expected = '$16.00'
+        actual = i18n.format_currency_with_options(16, 'USD', locale=i18n.Locale('en'), trailing_zeroes=True)
+        assert actual == expected
+
+    def test_format_currency_defaults_to_trailing_zeroes(self):
+        expected = '$16.00'
+        actual = i18n.format_currency_with_options(16, 'USD', locale=i18n.Locale('en'))
+        assert actual == expected
+
 
     # sru - safely_reserve_a_username