Skip to content

Commit

Permalink
th2_output: Export "-0.0" as "0.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed Feb 4, 2024
1 parent dd12861 commit 6255dae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion extensions/th2_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ def fstr_trim_zeros(s: str) -> str:
"""
Strip trailing zeros from a string that represents a floating point number.
"""
assert '.' in s
i = len(s) - 1
while s[i] == '0': i -= 1
if s[i] == '.': i += 1
return s[:i+1]
s = s[:i+1]
return "0.0" if s == "-0.0" else s


class Th2Line:
def __init__(self, type = 'wall'):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_th2_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import th2_output as m
import pytest


def test_fstr():
assert m.fstr(1.23456) == "1.2346"
assert m.fstr(1.2) == "1.2"
assert m.fstr(1) == "1.0"
assert m.fstr(-1e-9) == "0.0"


def test_fstr2():
assert m.fstr2(1.23456789) == "1.23456789"
assert m.fstr2(1.23456789, dbl_dig=5) == "1.2346"
assert m.fstr2(123.456789, dbl_dig=5) == "123.46"
assert m.fstr2(1.23456789, max_dig=2) == "1.23"
assert m.fstr2(1, dbl_dig=5) == "1.0"
assert m.fstr2(1, max_dig=2) == "1.0"
assert m.fstr2(-1e-9, max_dig=7) == "0.0"


def test_fstr_trim_zeros():
assert m.fstr_trim_zeros("1.23") == "1.23"
assert m.fstr_trim_zeros("1.2300") == "1.23"
assert m.fstr_trim_zeros("1.0000") == "1.0"
assert m.fstr_trim_zeros("1.0") == "1.0"
assert m.fstr_trim_zeros("-0.000") == "0.0"
with pytest.raises(Exception):
m.fstr_trim_zeros("123")

0 comments on commit 6255dae

Please sign in to comment.