Kaydet (Commit) 9d1a6957 authored tarafından Eric Rouleau's avatar Eric Rouleau Kaydeden (comit) Tim Graham

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

üst adacbd64
from decimal import Decimal
from django.conf import settings from django.conf import settings
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six from django.utils import six
...@@ -22,7 +24,10 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='', ...@@ -22,7 +24,10 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
return mark_safe(six.text_type(number)) return mark_safe(six.text_type(number))
# sign # sign
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] == '-': if str_number[0] == '-':
sign = '-' sign = '-'
str_number = str_number[1:] str_number = str_number[1:]
......
from decimal import Decimal
from sys import float_info
from unittest import TestCase from unittest import TestCase
from django.utils.numberformat import format as nformat from django.utils.numberformat import format as nformat
from sys import float_info
class TestNumberFormat(TestCase): class TestNumberFormat(TestCase):
...@@ -45,3 +47,12 @@ class TestNumberFormat(TestCase): ...@@ -45,3 +47,12 @@ class TestNumberFormat(TestCase):
self.assertEqual(nformat(0 - int_max, '.'), most_max.format('-', '8')) self.assertEqual(nformat(0 - int_max, '.'), most_max.format('-', '8'))
self.assertEqual(nformat(-1 - int_max, '.'), most_max.format('-', '9')) self.assertEqual(nformat(-1 - int_max, '.'), most_max.format('-', '9'))
self.assertEqual(nformat(-2 * int_max, '.'), most_max2.format('-')) 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')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment