jazzband/prettytable

`float_format` not working

watermelon0guy opened this issue · 1 comments

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?

  • OS: Fedora 40
  • Python: 3.11
  • PrettyTable: 3.10.0

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 |
+------------------------+-----------------------+-----------------------+

Thanks for the report, this looks like a duplicate of #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 |
+------------+------------+------------+