[Git][NTPsec/ntpsec][master] Fixed fitinfield() to round numbers instead of cropping.
Gary E. Miller
gitlab at mg.gitlab.com
Wed Apr 12 01:12:04 UTC 2017
Gary E. Miller pushed to branch master at NTPsec / ntpsec
Commits:
8e81a885 by Ian Bruene at 2017-04-11T19:45:33-05:00
Fixed fitinfield() to round numbers instead of cropping.
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -316,12 +316,20 @@ def fitinfield(value, fieldsize):
elif vallen < fieldsize: # Extra room, pad it out
pad = " " * (fieldsize - vallen)
newvalue = pad + value
- else: # Insufficient room, crop as few digits as possible
- above, below, neg = breaknumberstring(value)
+ else: # Insufficient room, round as few digits as possible
diff = vallen - fieldsize
- bellen = len(below)
- croplen = min(bellen, diff) # Never crop above the decimal point
- newvalue = value[:-croplen]
+ declen = len(value.split(".")[1]) # length of decimals
+ croplen = min(declen, diff) # Never round above the decimal point
+ roundlen = declen - croplen # How many digits we round to
+ newvalue = str(round(float(value), roundlen))
+ splitted = newvalue.split(".")
+ declen = len(splitted[1])
+ if roundlen == 0: # if rounding all the decimals don't display .0
+ # but do display the point, to show that there is more beyond
+ newvalue = splitted[0] + "."
+ elif roundlen > declen: # some zeros have been cropped, fix that
+ padcount = roundlen - declen
+ newvalue = newvalue + ("0" * padcount)
return newvalue
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -249,15 +249,19 @@ class TestPylibUtilMethods(unittest.TestCase):
f = ntp.util.fitinfield
# Field too small, crop decimals
- self.assertEqual(f("123.456", 5), "123.4")
+ self.assertEqual(f("123.456", 5), "123.5")
# ditto, negative
self.assertEqual(f("-123.456", 5), "-123.")
# Field too small, blow field
- self.assertEqual(f("12345.678", 4), "12345.")
+ self.assertEqual(f("12345.678", 4), "12346.")
# Goldilocks
self.assertEqual(f("123.456", 7), "123.456")
# Field too big
self.assertEqual(f("123.456", 10), " 123.456")
+ # Rounding test, round down
+ self.assertEqual(f("1.994", 4), "1.99")
+ # Rounding test, round up
+ self.assertEqual(f("1.995", 4), "2.00")
def test_cropprecision(self):
f = ntp.util.cropprecision
@@ -315,7 +319,7 @@ class TestPylibUtilMethods(unittest.TestCase):
" 23ns")
# Different units
self.assertEqual(f("12.345", nu.UNITS_PPX, nu.UNIT_PPM),
- "12.34ppm")
+ "12.35ppm")
# Strip
self.assertEqual(f("1.23", nu.UNITS_SEC, nu.UNIT_MS, strip=True),
"1.23ms")
@@ -332,7 +336,7 @@ class TestPylibUtilMethods(unittest.TestCase):
# Scale
self.assertEqual(ntp.util.stringfiltcooker(
"1000.02 3400.5 0.67835 -23.0 9001 6.7 1.00 1234"),
- "1.00002 3.4005 0.00067 -0.0230 9.001 0.0067 0.00100 1.234 s"
+ "1.00002 3.4005 0.00068 -0.0230 9.001 0.0067 0.00100 1.234 s"
)
if __name__ == '__main__':
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/8e81a8859559087a25d37b85dd04f35efce21e7f
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170412/bcaef476/attachment.html>
More information about the vc
mailing list