Skip to content

Override units display order (1 kW·h displayed as 1 h·kW) #1841

Closed
@scanzy

Description

@scanzy

Let's consider this code

import pint as pt

# sets compact display mode
u = pt.UnitRegistry()
u.default_format = '~P'

# creates quantity
q = u.Quantity("1 kW * 1 h")

# this prints "1 h·kW", not "1 kW·h" as expected
print(q)

During the string conversion I believe the units are sorted alphabetically, in fact hour comes before kilowatt.

How can I override this behaviour to have it printed correctly as 1 kW·h?
For now I am doing str(q).replace("h·kW", "kW·h") but I am looking for a better solution, that may act globally and before string conversion.

Any suggestions?
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions