[Git][NTPsec/ntpsec][master] Fix for ungarded decimal split.

Gary E. Miller gitlab at mg.gitlab.com
Thu Apr 13 23:49:29 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
808b0526 by Ian Bruene at 2017-04-13T23:49:18+00:00
Fix for ungarded decimal split.

- - - - -


1 changed file:

- pylib/util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -242,19 +242,22 @@ def fitinfield(value, fieldsize):
         pad = " " * (fieldsize - vallen)
         newvalue = pad + value
     else:  # Insufficient room, round as few digits as possible
-        diff = vallen - fieldsize
-        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)
+        if "." in value:  # Ok, we *do* have decimals to crop
+            diff = vallen - fieldsize
+            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(".")  # This should never fail
+            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)
+        else:  # No decimals, nothing we can crop
+            newvalue = value
     return newvalue
 
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/808b0526cd870a7792c27ebdd4fdc8d3750407fd

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/808b0526cd870a7792c27ebdd4fdc8d3750407fd
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170413/514bbbf6/attachment.html>


More information about the vc mailing list