diff --git a/pynumeral/pynumeral.py b/pynumeral/pynumeral.py index 0f669a6..c9c963c 100644 --- a/pynumeral/pynumeral.py +++ b/pynumeral/pynumeral.py @@ -80,6 +80,7 @@ def get_suffix(self, numeralfmt, value): def get_python_format(self, numeralfmt, value): thousand = "," if "," in numeralfmt else "" float_or_exp = "e" if "e+0" in numeralfmt else "f" + precision = numeralfmt.split(".")[1] if "." in numeralfmt else None if re.match("^0{2,}$", numeralfmt.split(",")[0]): fmt = "{:0%s%s}" % ( len(numeralfmt.split(",")[0]) @@ -89,15 +90,15 @@ def get_python_format(self, numeralfmt, value): ) value = int(value) else: - if "." in numeralfmt: - decimals = len( - list( - filter( - lambda c: c == "0", - numeralfmt.split(".")[1].replace("e+0", ""), - ) + if precision is not None: + if "[" in numeralfmt and "[.]" not in numeralfmt: + precision = precision.replace("]", "").split("[") + split_val = str(value).split(".") + decimals = 0 if len(split_val) == 1 else precision[1].count("0") + else: + decimals = len( + list(filter(lambda c: c == "0", precision.replace("e+0", ""))) ) - ) else: decimals = 0 plus = "+" if re.match(".*(^|[^e])\\+.*", numeralfmt) else "-" diff --git a/tests/test_pynumeral.py b/tests/test_pynumeral.py index d92d991..fb967c0 100644 --- a/tests/test_pynumeral.py +++ b/tests/test_pynumeral.py @@ -60,3 +60,4 @@ def test_all(): assert pynumeral.format(0.000123987, "0.000e+0") == "1.240e-04" assert pynumeral.format(1, "0 a") == "1 " assert pynumeral.format(-1, "00") == "-01" + assert pynumeral.format(1, "0,0.[000]") == "1"