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

float_format with % #99

Closed
MicroElf opened this issue Mar 22, 2021 · 8 comments · Fixed by #136
Closed

float_format with % #99

MicroElf opened this issue Mar 22, 2021 · 8 comments · Fixed by #136

Comments

@MicroElf
Copy link

MicroElf commented Mar 22, 2021

I can't find the way to show float values with % (0.0125 as 1,25% for example)

I tried:

table.float_format = '%0.2f' #(which should be the right way I suppose)
table.float_format = '%0.2'
table.float_format = '0.2%f'
table.float_format = '0.2%'

But every time there is an exception raised:
Exception: Invalid value for float_format! Must be a float format string.

What am I doing wrong or is it a bug?

@hugovk
Copy link
Member

hugovk commented Mar 22, 2021

https://github.com/jazzband/prettytable#style-options says:

  • float_format - A string which controls the way floating point data is printed. This works like: print("%<float_format>f" % data)

None of the examples would work with plain percent formatting:

>>> print("%0.2f" % 0.0125)
0.01
>>> print("%0.2" % 0.0125)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print("0.2%f" % 0.0125)
0.20.012500
>>> print("0.2%" % 0.0125)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: incomplete format

This looks closest? Note the input value is also changed:

>>> print("%0.2f%%" % (0.0125*100))
1.25%

But note float_format provides the leading % and trailing f already, you can't also supply the %% at the very end. Which is to say percentage formatting isn't currently supported.

I suggest you pre-process your data into the required 1,25% form.

@MicroElf
Copy link
Author

MicroElf commented Mar 25, 2021

Thanks you hugovk!

%0.2f%% is what I was looking for. I thought there should be an elegant way to achieve this output with PrettyTable. Sadly it's apparently not. So, I'll follow your suggestion and format my float numbers where needed into string with % and then add it to PrettyTable.

float_format worthless in this case

It would be useful to support percentage formatting for float numbers imho

@hugovk
Copy link
Member

hugovk commented Mar 27, 2021

I'd be open to a PR to add support for that, either percent_format or perhaps a general purpose formatter would be more flexible?

@davzucky
Copy link
Contributor

davzucky commented Oct 2, 2021

@hugovk ,
I looked at the code, and I see you are applying the format like that

    def _format_value(self, field, value):
        if isinstance(value, int) and field in self._int_format:
            value = ("%%%sd" % self._int_format[field]) % value
        elif isinstance(value, float) and field in self._float_format:
            value = ("%%%sf" % self._float_format[field]) % value
        return str(value)

did you consider using something more like that self._float_format[field].format(value) where you let the consumer defining exactly what format to apply. For example for percent you could use "{:,.2%} and thousand and decimal {:,.2}

If you are happy to use .format I will prepare a PR for you

@hugovk
Copy link
Member

hugovk commented Oct 3, 2021

Thanks for the suggestion, my main concern would be not wanting to break backward compatibility for people using the existing formatting; this is a 12 year old library with nearly 4m downloads per month.

@davzucky
Copy link
Contributor

davzucky commented Oct 4, 2021

That make sense. I could imagine few other solutions:

  1. Add a new property custom_format which for each field a callable[[str, Any], str]. This would allow the consumer of PrettyTable to use any custom format they want. I like this version because you could do something like that
>>> "{:,} USD".format(12344556.45)
'12,344,556.45 USD'
  1. Add a flag with support python2 format, which could be used for few version the time everyone can migrate to the new format.

Do you think any of them could work ?

@hugovk
Copy link
Member

hugovk commented Oct 4, 2021

  1. Yep, this could work, like a general purpose formatter suggested above (float_format with % #99 (comment))

  2. Python 2 is nearly two years past EOL, we're not adding back support at this stage :)

@hugovk
Copy link
Member

hugovk commented Oct 8, 2021

@MicroElf Please can you have a look at PR #136 and see if it would address your needs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants