def test_basics(self):
     for number in range(1000):
         for extra in range(5):
             extra /= 10.0
             assert_equal(roundup(number + extra), number, +extra)
             assert_equal(roundup(number - extra), number, -extra)
         assert_equal(roundup(number + 0.5), number+1)
Example #2
0
 def test_basics(self):
     for number in range(1000):
         for extra in range(5):
             extra /= 10.0
             assert_equal(roundup(number + extra), number, +extra)
             assert_equal(roundup(number - extra), number, -extra)
         assert_equal(roundup(number + 0.5), number + 1)
Example #3
0
 def test_negative(self):
     for number in range(1000):
         number *= -1
         for extra in range(5):
             extra /= 10.0
             assert_equal(roundup(number + extra), number)
             assert_equal(roundup(number - extra), number)
         assert_equal(roundup(number - 0.5), number - 1)
 def test_negative(self):
     for number in range(1000):
         number *= -1
         for extra in range(5):
             extra /= 10.0
             assert_equal(roundup(number + extra), number)
             assert_equal(roundup(number - extra), number)
         assert_equal(roundup(number - 0.5), number-1)
Example #5
0
 def convert(self, format, millis=True):
     try:
         result_converter = getattr(self, '_convert_to_%s' % format.lower())
     except AttributeError:
         raise ValueError("Unknown format '%s'." % format)
     seconds = self.seconds if millis else float(roundup(self.seconds))
     return result_converter(seconds, millis)
 def convert(self, format, millis=True):
     try:
         result_converter = getattr(self, '_convert_to_%s' % format.lower())
     except AttributeError:
         raise ValueError("Unknown format '%s'." % format)
     seconds = self.seconds if millis else float(roundup(self.seconds))
     return result_converter(seconds, millis)
 def _convert_to_timestamp(self, dt, millis=True):
     if not millis:
         return dt.strftime('%Y-%m-%d %H:%M:%S')
     ms = roundup(dt.microsecond / 1000.0)
     if ms == 1000:
         dt += timedelta(seconds=1)
         ms = 0
     return dt.strftime('%Y-%m-%d %H:%M:%S') + '.%03d' % ms
Example #8
0
 def _convert_to_timestamp(self, dt, millis=True):
     if not millis:
         return dt.strftime('%Y-%m-%d %H:%M:%S')
     ms = roundup(dt.microsecond / 1000.0)
     if ms == 1000:
         dt += timedelta(seconds=1)
         ms = 0
     return dt.strftime('%Y-%m-%d %H:%M:%S') + '.%03d' % ms
 def test_round_even_down_when_negative(self):
     assert_equal(roundup(-0.5), -1)
     assert_equal(roundup(-5, -1), -10)
     assert_equal(roundup(-5.0, -1), -10)
     assert_equal(roundup(-500, -3), -1000)
     assert_equal(roundup(-0.05, 1), -0.1)
     assert_equal(roundup(-0.49951, 3), -0.5)
Example #10
0
 def test_round_even_down_when_negative(self):
     assert_equal(roundup(-0.5), -1)
     assert_equal(roundup(-5, -1), -10)
     assert_equal(roundup(-5.0, -1), -10)
     assert_equal(roundup(-500, -3), -1000)
     assert_equal(roundup(-0.05, 1), -0.1)
     assert_equal(roundup(-0.49951, 3), -0.5)
 def _seconds_to_datetime(self, secs):
     # Workaround microsecond rounding errors with IronPython:
     # https://github.com/IronLanguages/main/issues/1170
     # Also Jython had similar problems, but they seem to be fixed in 2.7.
     dt = datetime.fromtimestamp(secs)
     return dt.replace(microsecond=roundup(secs % 1 * 1e6))
Example #12
0
 def _time_as_seconds(self, millis):
     return str(roundup(millis, -3) // 1000)
 def test_ndigits_above_zero(self):
     assert_equal(roundup(0.1234, 1), 0.1)
     assert_equal(roundup(0.9999, 1), 1.0)
     assert_equal(roundup(0.9876, 3), 0.988)
 def test_problems(self):
     assert_equal(roundup(59.85, 1), 59.9)    # This caused #2872
     if not IRONPYTHON:
         assert_equal(roundup(1.15, 1), 1.1)  # 1.15 is actually 1.49999..
     else:
         assert_equal(roundup(1.15, 1), 1.2)  # but ipy still rounds it up
Example #15
0
 def _seconds_to_datetime(self, secs):
     # Workaround microsecond rounding errors with IronPython:
     # https://github.com/IronLanguages/main/issues/1170
     # TODO: can this be simplified now
     dt = datetime.fromtimestamp(secs)
     return dt.replace(microsecond=roundup(secs % 1 * 1e6))
Example #16
0
 def test_round_even_up(self):
     assert_equal(roundup(0.5), 1)
     assert_equal(roundup(5, -1), 10)
     assert_equal(roundup(500, -3), 1000)
     assert_equal(roundup(0.05, 1), 0.1)
     assert_equal(roundup(0.49951, 3), 0.5)
Example #17
0
 def test_ndigits_above_zero(self):
     assert_equal(roundup(0.1234, 1), 0.1)
     assert_equal(roundup(0.9999, 1), 1.0)
     assert_equal(roundup(0.9876, 3), 0.988)
Example #18
0
 def test_ndigits_below_zero(self):
     assert_equal(roundup(7, -1), 10)
     assert_equal(roundup(77, -1), 80)
     assert_equal(roundup(123, -2), 100)
     assert_equal(roundup(-1234, -2), -1200)
     assert_equal(roundup(9999, -2), 10000)
 def test_round_even_up(self):
     assert_equal(roundup(0.5), 1)
     assert_equal(roundup(5, -1), 10)
     assert_equal(roundup(500, -3), 1000)
     assert_equal(roundup(0.05, 1), 0.1)
     assert_equal(roundup(0.49951, 3), 0.5)
Example #20
0
 def test_problems(self):
     assert_equal(roundup(59.85, 1), 59.9)    # This caused #2872
     assert_equal(roundup(1.15, 1), 1.1)      # 1.15 is actually 1.49999..
Example #21
0
 def _time_as_seconds(self, millis):
     return str(roundup(millis, -3) // 1000)
 def test_ndigits_below_zero(self):
     assert_equal(roundup(7, -1), 10)
     assert_equal(roundup(77, -1), 80)
     assert_equal(roundup(123, -2), 100)
     assert_equal(roundup(-1234, -2), -1200)
     assert_equal(roundup(9999, -2), 10000)
Example #23
0
 def _seconds_to_datetime(self, secs):
     # Workaround microsecond rounding errors with IronPython:
     # https://github.com/IronLanguages/main/issues/1170
     # Also Jython had similar problems, but they seem to be fixed in 2.7.
     dt = datetime.fromtimestamp(secs)
     return dt.replace(microsecond=roundup(secs % 1 * 1e6))
Example #24
0
 def test_return_type(self):
     for n in [1, 1000, 0.1, 0.001]:
         for d in [-3, -2, -1, 0, 1, 2, 3]:
             assert_equal(type(roundup(n, d)), float if d > 0 else int)
             assert_equal(type(roundup(n, d, return_type=int)), int)
             assert_equal(type(roundup(n, d, return_type=float)), float)
Example #25
0
 def test_problems(self):
     assert_equal(roundup(59.85, 1), 59.9)  # This caused #2872
     if not IRONPYTHON:
         assert_equal(roundup(1.15, 1), 1.1)  # 1.15 is actually 1.49999..
     else:
         assert_equal(roundup(1.15, 1), 1.2)  # but ipy still rounds it up
 def test_return_type(self):
     for n in [1, 1000, 0.1, 0.001]:
         for d in [-3, -2, -1, 0, 1, 2, 3]:
             assert_equal(type(roundup(n, d)), float if d > 0 else int)
             assert_equal(type(roundup(n, d, return_type=int)), int)
             assert_equal(type(roundup(n, d, return_type=float)), float)