-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Comments
https://github.com/jazzband/prettytable#style-options says:
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 I suggest you pre-process your data into the required |
Thanks you hugovk!
It would be useful to support percentage formatting for float numbers imho |
I'd be open to a PR to add support for that, either |
@hugovk , 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 If you are happy to use |
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. |
That make sense. I could imagine few other solutions:
>>> "{:,} USD".format(12344556.45)
'12,344,556.45 USD'
Do you think any of them could work ? |
|
I can't find the way to show float values with % (0.0125 as 1,25% for example)
I tried:
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?
The text was updated successfully, but these errors were encountered: