Browse Source

Display "Humanly" rounded numbers

Jocelyn Delande 9 years ago
parent
commit
cf8e5a55f7

+ 2 - 2
costs/templates/costs/document_detail.html

@@ -36,7 +36,7 @@
       </td>
       <td>
         {% if cost.capacity_unit %}
-        {{ cost.total_capacity }} {{ cost.get_capacity_unit_display }}
+        {{ cost.total_capacity|human_round }} {{ cost.get_capacity_unit_display }}
         {% else %}
         n/a
         {% endif %}
@@ -71,7 +71,7 @@
     </td>
     <td>
         {% if good.capacity_unit %}
-          {{ good.total_capacity }} {{ good.get_capacity_unit_display }}
+          {{ good.total_capacity|human_round }} {{ good.get_capacity_unit_display }}
         {% else %}
           n/a
         {% endif %}

+ 2 - 2
costs/templates/costs/service_detail.html

@@ -12,7 +12,7 @@
        {{ usage.resource.name }}
       <span class="bare-info">
        {% if usage.resource.capacity_unit %}
-           ({{ usage.unit_share }} {{ usage.resource.get_capacity_unit_display }})
+           ({{ usage.unit_share|human_round }} {{ usage.resource.get_capacity_unit_display }})
          {% else %}
            ({{ usage.unit_real_share|percent }})
          {% endif %}
@@ -28,7 +28,7 @@
       {{ usage.resource.name }}
       <span class="bare-info">
                {% if usage.resource.capacity_unit %}
-           ({{ usage.unit_share }} {{ usage.resource.get_capacity_unit_display }})
+           ({{ usage.unit_share|human_round }} {{ usage.resource.get_capacity_unit_display }})
          {% else %}
            ({{ usage.unit_real_share|percent }})
          {% endif %}

+ 32 - 3
costs/templatetags/costs.py

@@ -8,11 +8,40 @@ def price(v):
     return '{:.2f}€'.format(v)
 
 
-@register.filter
-def round(v):
-    return '{:.1f}'.format(v)
+@register.filter(name='round')
+def _round(v, precision=1):
+    tpl = '{{:.{}f}}'.format(precision)
+    return tpl.format(v)
 
 
 @register.filter
 def percent(v):
     return format(v, '.0%')
+
+
+@register.filter
+def human_round(v, precision=2):
+    """ Displaying only <precision> meaningful digits,
+
+    Also remove trailing zeroes
+    """
+    if v == 0:
+        return str(int(0))
+    else:
+        _v = _round(v, 10)
+        int_part, decimal_part = _v.split('.')
+        if int(int_part) == 0:
+            reached_precision = 0
+        else:
+            reached_precision = len(int_part)
+
+        decimal_count = 0
+
+        for idx, dec in enumerate(decimal_part):
+            if reached_precision >= precision:
+                break
+
+            decimal_count += 1
+            if not ((reached_precision == 0) and (dec == '0')):
+                reached_precision += 1
+    return str(round(v, decimal_count)).rstrip('0').rstrip('.')

+ 23 - 0
costs/tests/test_templatetags.py

@@ -0,0 +1,23 @@
+import unittest
+
+from ..templatetags.costs import human_round
+
+
+class TestFilters(unittest.TestCase):
+    def test_human_round(self):
+
+        results = (
+            (0, '0'),
+            (0.1, '0.1'),
+            (0.12, '0.12'),
+            (1, '1'),
+            (1.12344589, '1.1'),
+            (1.19, '1.2'),
+            (0.00123, '0.0012'),
+            (0.00000000001, '0'),
+            (123, '123'),
+            (12.1, '12'),
+            )
+
+        for inp, outp in results:
+            self.assertEqual(human_round(inp), outp)