Commits

Christopher Peplin committed 8aa8f6d

When converting to a string, always show Quantities to 2 decimal places.

I haven't been able to track down exactly what change in Python 3 causes this,
but when a Quantity with the value '1' is rendered in Python 2.7 the result is
'1' and in Python 3 the result is '1.0'.

Since there is no difference in correctness, this commit updates changes the
semantics of Quantity's str method so it always returns 2 decimal places. I
updated the tests accordingly, and now the test suites works in Python 2.5, 2.6,
2.7 and 3.3.

  • Participants
  • Parent commits 2492a97

Comments (0)

Files changed (3)

units/quantity.py

         return Quantity(self.num ** exponent, self.unit ** exponent)
             
     def __str__(self):
-        if self.unit.str_includes_multiplier():
-            return str(self.num) + ' ' + str(self.unit)
-        else:
-            return str(self.num * self.unit.squeeze()) + ' ' + str(self.unit)
+        num = self.num
+        if not self.unit.str_includes_multiplier():
+            num *= self.unit.squeeze()
+        return '%0.2f %s' % (num, self.unit)
 
     def __repr__(self):
         return ("Quantity(" + 

units/tests/test_conversion.py

     
     assert one_km_in_m == Quantity(1000, unit('m'))
     assert one_km_in_m.unit == unit('m')
-    assert str(one_km_in_m) == '1000 m'
+    assert str(one_km_in_m) == '1000.00 m'
 
 def test_valid_named_to_composed():
     """Named units should convert to the composed equivalents."""
     one_hz_in_per_second = hertz.composed_unit(Quantity(1, hertz))
     
     assert one_hz_in_per_second == Quantity(1, hertz)
-    assert str(one_hz_in_per_second) == '1 1 / s'
-    
+    assert str(one_hz_in_per_second) == '1.00 1 / s'
+
 def test_valid_named_to_named():
     """Named units should convert to named equivalents."""    
 
     
     one_gray_in_sievert = sievert(Quantity(1, gray))
     assert one_gray_in_sievert == Quantity(1, gray)
-    assert str(one_gray_in_sievert) == '1 Sv'
+    assert str(one_gray_in_sievert) == '1.00 Sv'
 
 def test_valid_basic_to_basic():
     """Test idempotent conversion."""
     
     assert thousand_m_in_km == Quantity(1, unit('km'))
     assert thousand_m_in_km.unit == unit('km')
-    assert str(thousand_m_in_km) == '1 km'
+    assert str(thousand_m_in_km) == '1.00 km'
 
 def test_valid_composed_to_basic():
     """Composed units should convert to basic equivalents."""
     
     hundred_cm_in_metres = metre(Quantity(100, unit('cm')))
     assert hundred_cm_in_metres == Quantity(1.0, metre)
-    assert str(hundred_cm_in_metres) == '1.0 m'
-    
+    assert str(hundred_cm_in_metres) == '1.00 m'
+
 def test_valid_composed_to_composed():
     """Valid composed units in terms of others."""
     metric_vel = unit('km') / unit('h')
     
     highway_kph = Quantity(100, metric_vel)
     highway_mph = Quantity(62.1371192237334, imp_vel)
-    
-    assert str(highway_kph) == '100 km / h'
-    assert str(highway_mph) == '62.1371192237 mi / h'
-    
-    assert within_epsilon(imp_vel(highway_kph), 
+
+    assert str(highway_kph) == '100.00 km / h'
+    assert str(highway_mph) == '62.14 mi / h'
+
+    assert within_epsilon(imp_vel(highway_kph),
                           highway_mph)
 
-    assert str(imp_vel(highway_kph)) == '62.1371192237 mi / h'
+    assert str(imp_vel(highway_kph)) == '62.14 mi / h'
 
 def test_valid_composed_to_named():
     """Composed units should convert to named equivalents."""
     one_hz = hertz(Quantity(1, per_second))
 
     assert one_hz == Quantity(1, hertz)
-    assert str(one_hz) == '1 Hz'    
-    
+    assert str(one_hz) == '1.00 Hz'
+
 def test_convert_from_num():
     """Nums should convert into quantities."""
     

units/tests/test_string_reps.py

 
 def test_quantity_str():
     """User-friendly string representation of quantities."""
-    assert str(Quantity(1, unit('m'))) == "1 m"
-    
+    assert str(Quantity(1, unit('m'))) == "1.00 m"
+
 def test_leaf_unit_repr():
     """Developer-friendly string representation of leaf units."""
     assert repr(unit('m')) == "LeafUnit('m', True)"