jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.32k stars 149 forks source link

`float_format` not working #300

Closed watermelon0guy closed 2 months ago

watermelon0guy commented 2 months ago

What did you do?

Format floats in table using float_format

What did you expect to happen?

Expect suppressing scientific notation

What actually happened?

Nothing changed

What versions are you using?

Please include code that reproduces the issue.

import prettytable as pt

arr = [1.3310884083630156e-05, 6.655442041815078e-06, 5.546201701512565e-06]

table = pt.PrettyTable()
table.float_format = "0.8"

table.add_row(arr)

print(table)
+------------------------+-----------------------+-----------------------+
|        Field 1         |        Field 2        |        Field 3        |
+------------------------+-----------------------+-----------------------+
| 1.3310884083630156e-05 | 6.655442041815078e-06 | 5.546201701512565e-06 |
+------------------------+-----------------------+-----------------------+
hugovk commented 2 months ago

Thanks for the report, this looks like a duplicate of https://github.com/jazzband/prettytable/issues/243.

As a workaround, set the format after adding the rows:

import prettytable as pt

arr = [1.3310884083630156e-05, 6.655442041815078e-06, 5.546201701512565e-06]

table = pt.PrettyTable()
table.add_row(arr)

table.float_format = "0.8"

print(table)
+------------+------------+------------+
|  Field 1   |  Field 2   |  Field 3   |
+------------+------------+------------+
| 0.00001331 | 0.00000666 | 0.00000555 |
+------------+------------+------------+