Coder Social home page Coder Social logo

float_format with % about prettytable HOT 8 CLOSED

MicroElf avatar MicroElf commented on June 27, 2024
float_format with %

from prettytable.

Comments (8)

davzucky avatar davzucky commented on June 27, 2024 2

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 ?

from prettytable.

hugovk avatar hugovk commented on June 27, 2024 1
  1. Yep, this could work, like a general purpose formatter suggested above (#99 (comment))

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

from prettytable.

hugovk avatar hugovk commented on June 27, 2024

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.

from prettytable.

MicroElf avatar MicroElf commented on June 27, 2024

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

from prettytable.

hugovk avatar hugovk commented on June 27, 2024

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?

from prettytable.

davzucky avatar davzucky commented on June 27, 2024

@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

from prettytable.

hugovk avatar hugovk commented on June 27, 2024

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.

from prettytable.

hugovk avatar hugovk commented on June 27, 2024

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

from prettytable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.