-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
float_format does not seem to work #243
Comments
Please can you post a complete, runnable snippet? For example, replace For good tips, see https://stackoverflow.com/help/minimal-reproducible-example |
Try setting the float_format after the rows are added. |
@hugovk sorry for replying so late. here some examples >>> x = prettytable.PrettyTable(float_format="10.2f") #same with 10.2f, .2f, .2
>>> x.field_names = ["Metric", "Initial sol.", "Best sol."]
>>> x.add_rows([["foo", 1.0/3., 1.0/3.]])
>>> x
+--------+--------------------+--------------------+
| Metric | Initial sol. | Best sol. |
+--------+--------------------+--------------------+
| foo | 0.3333333333333333 | 0.3333333333333333 |
+--------+--------------------+--------------------+
>>> x = prettytable.PrettyTable(float_format=".2f") #same with 10.2f, .2f, .2
>>> x.field_names = ["Metric", "Initial sol.", "Best sol."]
>>> x.add_rows([["foo", 1.0/3., 1.0/3.]])
>>> x
+--------+--------------------+--------------------+
| Metric | Initial sol. | Best sol. |
+--------+--------------------+--------------------+
| foo | 0.3333333333333333 | 0.3333333333333333 |
+--------+--------------------+--------------------+
>>> x.add_rows([["foo", 1.0/3., 1.0e5/3.]])
>>> x
+--------+--------------------+--------------------+
| Metric | Initial sol. | Best sol. |
+--------+--------------------+--------------------+
| foo | 0.3333333333333333 | 0.3333333333333333 |
| foo | 0.3333333333333333 | 33333.333333333336 |
+--------+--------------------+--------------------+
>>>
@ajgringo619 it works as you suggested >>> x.float_format = "10.2f"
>>> x
+--------+--------------+-------------+
| Metric | Initial sol. | Best sol. |
+--------+--------------+-------------+
| foo | 0.33f | 0.33f |
| foo | 0.33f | 33333.33f |
+--------+--------------+-------------+ Seems a bug to me. |
Thanks, and as a standalone script: import prettytable
x = prettytable.PrettyTable(float_format="10.2f") # same with 10.2f, .2f, .2
x.field_names = ["Metric", "Initial sol.", "Best sol."]
x.add_rows([["foo", 1.0 / 3.0, 1.0 / 3.0]])
print(x)
x = prettytable.PrettyTable()
x.field_names = ["Metric", "Initial sol.", "Best sol."]
x.add_rows([["foo", 1.0 / 3.0, 1.0 / 3.0]])
x.float_format = "10.2f"
print(x) Looks like the problem is in here, @float_format.setter
def float_format(self, val):
if val is None or (isinstance(val, dict) and len(val) == 0):
self._float_format = {}
else:
self._validate_option("float_format", val)
for field in self._field_names:
self._float_format[field] = val Called from |
What did you do?
I would like to setup the float format for the whole table using the
float_format
option in thePrettyTable
class constructor.I have tried several values, but either I get an error because the value is not a float format string (fair), or nothing happens.
What did you expect to happen?
What actually happened?
What versions are you using?
Python 3.11.3 (main, Apr 7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Please include code that reproduces the issue.
The text was updated successfully, but these errors were encountered: