From df193b3cefda982dfcd4813bf4e2ac09b1358f21 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 9 Mar 2015 17:56:13 +0100 Subject: [PATCH] Fixed #24382 -- Allowed unicode chars inside formatted numbers Thanks Jacob Rief for the report and Tim Graham for the review. --- django/utils/numberformat.py | 2 ++ tests/utils_tests/test_numberformat.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py index 3f61a6600a..6667d823a5 100644 --- a/django/utils/numberformat.py +++ b/django/utils/numberformat.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from decimal import Decimal from django.conf import settings diff --git a/tests/utils_tests/test_numberformat.py b/tests/utils_tests/test_numberformat.py index 43579060bc..d963bd284b 100644 --- a/tests/utils_tests/test_numberformat.py +++ b/tests/utils_tests/test_numberformat.py @@ -1,3 +1,6 @@ +# -*- encoding: utf-8 -*- +from __future__ import unicode_literals + from decimal import Decimal from sys import float_info from unittest import TestCase @@ -59,3 +62,15 @@ class TestNumberFormat(TestCase): self.assertEqual(nformat(Decimal('1234'), '.', grouping=2, thousand_sep=',', force_grouping=True), '12,34') self.assertEqual(nformat(Decimal('-1234.33'), '.', decimal_pos=1), '-1234.3') self.assertEqual(nformat(Decimal('0.00000001'), '.', decimal_pos=8), '0.00000001') + + def test_decimal_subclass(self): + class EuroDecimal(Decimal): + """ + Wrapper for Decimal which prefixes each amount with the € symbol. + """ + def __format__(self, specifier, **kwargs): + amount = super(EuroDecimal, self).__format__(specifier, **kwargs) + return '€ {}'.format(amount) + + price = EuroDecimal('1.23') + self.assertEqual(nformat(price, ','), '€ 1,23')