Skip to content
Discussion options

You must be logged in to vote

That library drops them by design: DecimalNumber keeps the digits as one integer plus a count of decimals and trims trailing zeros, so DecimalNumber("64000.00") holds 64000 and prints 64000. It has no method that prints a fixed number of places; to_string_thousands() and to_string_max_length() leave the zeros off too.

For money it's simpler to go through whole cents, which also rounds the last digit instead of cutting it off (fix_trailing() truncates, so 2.675 comes out as 2.67):

def money(n, places=2):
    q = (abs(n) * (10 ** places)).to_int_round()
    s = str(q)
    if places:
        s = "0" * (places + 1 - len(s)) + s
        s = s[:-places] + "." + s[-places:]
    return ("-" if q and

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ctimmer
Comment options

@scruss
Comment options

Answer selected by ctimmer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants