pythondjangohumanize

Python Number to Word e.g 10K, 10.65M


I've looked into a few ways of representing numbers in a small space (in Django) and whilst their django.contrib.humanize.intword works great, i'd prefer if it said M instead of million for example. I know this wouldn't work with exceptionally large numbers (10^15 and 10^18 both start with Q).

I basically need four different unit names up to trillion: K, M, B and T. Are there any packages that allow a lot of configuration with numbers?

I've not posted any code because what I have works but not quite how i'd like it, this is just a question to see if there is a better way to get the numbers i'm hoping for.


Solution

  • You should be able to edit the code in humanize.intword on your local machine. If I were doing this I would make a copy of the code and rename it. Make adjustments as you need them and call the renamed version. The source should be on your machine humanize.intword

    So something like (original)

    (6, lambda number: (
        ungettext('%(value).1f million', '%(value).1f million', number),
        ungettext('%(value)s million', '%(value)s million', number),
    )),
    

    Change to the representation you need