Skip to content

Commit

Permalink
fix float precision
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadari committed Apr 14, 2022
1 parent ad23d37 commit dac53ae
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions tabulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import print_function
from __future__ import unicode_literals
from collections import namedtuple
from decimal import Decimal
from decimal import Decimal, getcontext, setcontext, Context
import sys
import re
import math
Expand Down Expand Up @@ -993,11 +993,23 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
val, (_text_type, _binary_type)
)
if is_a_colored_number:
print("HIIIIII", val)
raw_val = _strip_invisible(val)
formatted_val = format(Decimal(raw_val), floatfmt)
formatted_val = format(Decimal(str(raw_val)), floatfmt)
return val.replace(raw_val, formatted_val)
else:
return format(Decimal(val), floatfmt)
context = Context(clamp=1, prec=6)
setcontext(context)
try:
if "f" in floatfmt:
if int(Decimal(val)) == Decimal(val):
val = int(Decimal(val, context=context))
val = Decimal(str(val), context=context)
else:
val = float(val)
except (OverflowError, ValueError):
val = float(val)
return format(val, floatfmt)
else:
return "{0}".format(val)

Expand Down

0 comments on commit dac53ae

Please sign in to comment.