Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ticket 18800: Template cannot format integers bigger than maximum float #358

Closed
wants to merge 2 commits into from

2 participants

Brad Pitcher Florian Apolloner
Brad Pitcher

https://code.djangoproject.com/ticket/18800

This pull includes a fixed version of the original patch, plus tests which fail before patching and pass afterward.

Florian Apolloner
Owner

Fixed in 319e135. Thanks!

Florian Apolloner apollo13 closed this September 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Sep 08, 2012
Brad Pitcher fix patch contributed by jbvsmo 6174cc4
Brad Pitcher numberformat tests for #18800 04d4c47
This page is out of date. Refresh to see the latest.
7  django/utils/numberformat.py
@@ -21,12 +21,10 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
21 21
     if isinstance(number, int) and not use_grouping and not decimal_pos:
22 22
         return mark_safe(six.text_type(number))
23 23
     # sign
24  
-    if float(number) < 0:
25  
-        sign = '-'
26  
-    else:
27  
-        sign = ''
  24
+    sign = ''
28 25
     str_number = six.text_type(number)
29 26
     if str_number[0] == '-':
  27
+        sign = '-'
30 28
         str_number = str_number[1:]
31 29
     # decimal part
32 30
     if '.' in str_number:
@@ -48,4 +46,3 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
48 46
             int_part_gd += digit
49 47
         int_part = int_part_gd[::-1]
50 48
     return sign + int_part + dec_part
51  
-
47  tests/regressiontests/utils/numberformat.py
... ...
@@ -0,0 +1,47 @@
  1
+from unittest import TestCase
  2
+from django.utils.numberformat import format as nformat
  3
+from sys import float_info
  4
+
  5
+
  6
+class TestNumberFormat(TestCase):
  7
+
  8
+    def test_format_number(self):
  9
+        self.assertEqual(nformat(1234, '.'), '1234')
  10
+        self.assertEqual(nformat(1234.2, '.'), '1234.2')
  11
+        self.assertEqual(nformat(1234, '.', decimal_pos=2), '1234.00')
  12
+        self.assertEqual(nformat(1234, '.', grouping=2, thousand_sep=','),
  13
+                         '1234')
  14
+        self.assertEqual(nformat(1234, '.', grouping=2, thousand_sep=',',
  15
+                                 force_grouping=True), '12,34')
  16
+        self.assertEqual(nformat(-1234.33, '.', decimal_pos=1), '-1234.3')
  17
+
  18
+    def test_format_string(self):
  19
+        self.assertEqual(nformat('1234', '.'), '1234')
  20
+        self.assertEqual(nformat('1234.2', '.'), '1234.2')
  21
+        self.assertEqual(nformat('1234', '.', decimal_pos=2), '1234.00')
  22
+        self.assertEqual(nformat('1234', '.', grouping=2, thousand_sep=','),
  23
+                         '1234')
  24
+        self.assertEqual(nformat('1234', '.', grouping=2, thousand_sep=',',
  25
+                                 force_grouping=True), '12,34')
  26
+        self.assertEqual(nformat('-1234.33', '.', decimal_pos=1), '-1234.3')
  27
+
  28
+    def test_large_number(self):
  29
+        most_max = ('{0}179769313486231570814527423731704356798070567525844996'
  30
+                    '598917476803157260780028538760589558632766878171540458953'
  31
+                    '514382464234321326889464182768467546703537516986049910576'
  32
+                    '551282076245490090389328944075868508455133942304583236903'
  33
+                    '222948165808559332123348274797826204144723168738177180919'
  34
+                    '29988125040402618412485836{1}')
  35
+        most_max2 = ('{0}35953862697246314162905484746340871359614113505168999'
  36
+                     '31978349536063145215600570775211791172655337563430809179'
  37
+                     '07028764928468642653778928365536935093407075033972099821'
  38
+                     '15310256415249098018077865788815173701691026788460916647'
  39
+                     '38064458963316171186642466965495956524082894463374763543'
  40
+                     '61838599762500808052368249716736')
  41
+        int_max = int(float_info.max)
  42
+        self.assertEqual(nformat(int_max, '.'), most_max.format('', '8'))
  43
+        self.assertEqual(nformat(int_max + 1, '.'), most_max.format('', '9'))
  44
+        self.assertEqual(nformat(int_max * 2, '.'), most_max2.format(''))
  45
+        self.assertEqual(nformat(0 - int_max, '.'), most_max.format('-', '8'))
  46
+        self.assertEqual(nformat(-1 - int_max, '.'), most_max.format('-', '9'))
  47
+        self.assertEqual(nformat(-2 * int_max, '.'), most_max2.format('-'))
1  tests/regressiontests/utils/tests.py
@@ -21,6 +21,7 @@
21 21
 from .ipv6 import TestUtilsIPv6
22 22
 from .jslex import JsToCForGettextTest, JsTokensTest
23 23
 from .module_loading import CustomLoader, DefaultLoader, EggLoader
  24
+from .numberformat import TestNumberFormat
24 25
 from .os_utils import SafeJoinTests
25 26
 from .regex_helper import NormalizeTests
26 27
 from .simplelazyobject import TestUtilsSimpleLazyObject
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.