Skip to content

Change repr(float) if float(int(f)) == f #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix 103
  • Loading branch information
Tim-St committed Sep 29, 2019
commit f5cd9f26f27f86d532bdd2ae176c1fcd007a3201
7 changes: 6 additions & 1 deletion py/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func FloatNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
}

func (a Float) M__str__() (Object, error) {
return String(fmt.Sprintf("%g", a)), nil
s := fmt.Sprintf("%g", a)
if idx := strings.IndexByte(s, '.'); idx == -1 {
// Sprintf implementation could change, so it's safer to check for '.'
s += ".0"
}
return String(s), nil
}

func (a Float) M__repr__() (Object, error) {
Expand Down
6 changes: 6 additions & 0 deletions py/tests/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
assert float("-1E9") == -1E9
assert float("1E400") == float("inf")
assert float(" -1E400") == float("-inf")
assert repr(float("1.0")) == "1.0"
assert repr(float("1.")) == "1.0"
assert repr(float("1.1")) == "1.1"
assert repr(float("1.11")) == "1.11"
assert repr(float("-1.0")) == "-1.0"
assert repr(float("1.00101")) == "1.00101"
assertRaises(ValueError, float, "1 E200")

doc="finished"