Skip to content
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

fix decimal precision issue #176

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion tabulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import print_function
from __future__ import unicode_literals
from collections import namedtuple
from decimal import Decimal
import sys
import re
import math
Expand Down Expand Up @@ -996,7 +997,14 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
formatted_val = format(float(raw_val), floatfmt)
return val.replace(raw_val, formatted_val)
else:
return format(float(val), floatfmt)
try:
if "f" in floatfmt and float('-inf') < float(val) < float('inf'):
val = Decimal(str(val))
else:
val = float(val)
except (OverflowError, ValueError):
val = float(val)
return format(val, floatfmt)
else:
return "{0}".format(val)

Expand Down
6 changes: 6 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,12 @@ def test_floatfmt_multi():
assert_equal(expected, result)


def test_floatfmt_precision():
result = tabulate([["99999998999.999980", 1234.5, 1.2345678, "inf"]], floatfmt=".6f", tablefmt="plain")
expected = "99999998999.999980 1234.500000 1.234568 inf"
assert_equal(expected, result)


def test_colalign_multi():
"Output: string columns with custom colalign"
result = tabulate(
Expand Down