Skip to content

Commit

Permalink
Fix 0,0.[000] case to address optimdata#1
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Recachinas committed Sep 18, 2020
1 parent 52568f9 commit 4c15964
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pynumeral/pynumeral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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 "-"
Expand Down
1 change: 1 addition & 0 deletions tests/test_pynumeral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 4c15964

Please sign in to comment.