Skip to content
Commits on Source (2)
......@@ -181,6 +181,34 @@ def oomsbetweenunits(a, b):
return abs((a - b) * 3)
def breaknumberstring(value):
"Breaks a number string into (aboveDecimal, belowDecimal, isNegative?)"
if value[0] == "-":
value = value[1:]
negative = True
else:
negative = False
if "." in value:
above, below = value.split(".")
else:
above = value
below = ""
return (above, below, negative)
def gluenumberstring(above, below, isnegative):
"Glues together parts of a number string"
if above == "":
above = "0"
if len(below) > 0:
newvalue = ".".join((above, below))
else:
newvalue = above
if isnegative is True:
newvalue = "-" + newvalue
return newvalue
def rescalestring(value, unitsscaled):
"Rescale a number string by a given number of units"
if unitsscaled == 0:
......
......@@ -211,5 +211,38 @@ class TestPylibUtilMethods(unittest.TestCase):
# ditto, negative
self.assertEqual(f("-1.23456", -2), "-1234560")
def test_breaknumberstring(self):
f = ntp.util.breaknumberstring
# No decimals, positive
self.assertEqual(f("1234567890"), ("1234567890", "", False))
# ditto, negative
self.assertEqual(f("-1234567890"), ("1234567890", "", True))
# No whole, positive
self.assertEqual(f(".12345"), ("", "12345", False))
# ditto, negative
self.assertEqual(f("-.12345"), ("", "12345", True))
# Both sides, position
self.assertEqual(f("123.456"), ("123", "456", False))
# ditto, negative
self.assertEqual(f("-123.456"), ("123", "456", True))
def test_gluenumberstring(self):
f = ntp.util.gluenumberstring
# No decimals, positive
self.assertEqual(f("123", "", False), "123")
# ditto, negative
self.assertEqual(f("123", "", True), "-123")
# No whole, positive
self.assertEqual(f("", "456", False), "0.456")
# ditto, negative
self.assertEqual(f("", "456", True), "-0.456")
# Both sides, positive
self.assertEqual(f("123", "456", False), "123.456")
# ditto, negative
self.assertEqual(f("123", "456", True), "-123.456")
if __name__ == '__main__':
unittest.main()