1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #23935 -- Converted decimals to fixed point in utils.numberformat.format

This commit is contained in:
Eric Rouleau 2014-11-30 03:29:52 -05:00 committed by Tim Graham
parent adacbd64a0
commit 9d1a69579b
2 changed files with 19 additions and 3 deletions

View File

@ -1,3 +1,5 @@
from decimal import Decimal
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils import six
@ -22,7 +24,10 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
return mark_safe(six.text_type(number))
# sign
sign = ''
str_number = six.text_type(number)
if isinstance(number, Decimal):
str_number = '{:f}'.format(number)
else:
str_number = six.text_type(number)
if str_number[0] == '-':
sign = '-'
str_number = str_number[1:]

View File

@ -1,6 +1,8 @@
from unittest import TestCase
from django.utils.numberformat import format as nformat
from decimal import Decimal
from sys import float_info
from unittest import TestCase
from django.utils.numberformat import format as nformat
class TestNumberFormat(TestCase):
@ -45,3 +47,12 @@ class TestNumberFormat(TestCase):
self.assertEqual(nformat(0 - int_max, '.'), most_max.format('-', '8'))
self.assertEqual(nformat(-1 - int_max, '.'), most_max.format('-', '9'))
self.assertEqual(nformat(-2 * int_max, '.'), most_max2.format('-'))
def test_decimal_numbers(self):
self.assertEqual(nformat(Decimal('1234'), '.'), '1234')
self.assertEqual(nformat(Decimal('1234.2'), '.'), '1234.2')
self.assertEqual(nformat(Decimal('1234'), '.', decimal_pos=2), '1234.00')
self.assertEqual(nformat(Decimal('1234'), '.', grouping=2, thousand_sep=','), '1234')
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')