Skip to content
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

In str.format "{0:#.5g}" for decimal.Decimal doesn't print trailing zeros #58046

Closed
py-user mannequin opened this issue Jan 22, 2012 · 5 comments
Closed

In str.format "{0:#.5g}" for decimal.Decimal doesn't print trailing zeros #58046

py-user mannequin opened this issue Jan 22, 2012 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@py-user
Copy link
Mannequin

py-user mannequin commented Jan 22, 2012

BPO 13838
Nosy @mdickinson, @ericvsmith, @skrah, @py-user
Superseder
  • bpo-7094: Add alternate float formatting styles to new-style formatting.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-01-23.15:18:19.886>
    created_at = <Date 2012-01-22.22:25:29.728>
    labels = ['interpreter-core', 'type-bug', 'library']
    title = 'In str.format "{0:#.5g}" for decimal.Decimal doesn\'t print trailing zeros'
    updated_at = <Date 2012-01-23.15:18:19.885>
    user = 'https://github.com/py-user'

    bugs.python.org fields:

    activity = <Date 2012-01-23.15:18:19.885>
    actor = 'eric.smith'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-01-23.15:18:19.886>
    closer = 'eric.smith'
    components = ['Interpreter Core', 'Library (Lib)']
    creation = <Date 2012-01-22.22:25:29.728>
    creator = 'py.user'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 13838
    keywords = []
    message_count = 5.0
    messages = ['151790', '151791', '151793', '151803', '151804']
    nosy_count = 4.0
    nosy_names = ['mark.dickinson', 'eric.smith', 'skrah', 'py.user']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '7094'
    type = 'behavior'
    url = 'https://bugs.python.org/issue13838'
    versions = ['Python 3.2']

    @py-user
    Copy link
    Mannequin Author

    py-user mannequin commented Jan 22, 2012

    http://docs.python.org/py3k/library/string.html#format-specification-mini-language

    The '#' option:
    "For floats, complex and Decimal the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result."

    1)
    >>> import decimal
    >>> '{0:#.5g}'.format(1.5)
    '1.5000'
    >>> '{0:.5f}'.format(decimal.Decimal(1.5))
    '1.50000'
    >>> '{0:.5g}'.format(decimal.Decimal(1.5))
    '1.5'
    >>> '{0:#.5g}'.format(decimal.Decimal(1.5))
    '1.5'
    >>>

    no zeros with "#"

    2)
    >>> import decimal
    >>> '{0:#.5g}'.format(decimal.Decimal('1.500000000000'))
    '1.5000'
    >>> '{0:.5g}'.format(decimal.Decimal('1.500000000000'))
    '1.5000'
    >>>

    zeros without "#"

    @py-user py-user mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 22, 2012
    @ericvsmith
    Copy link
    Member

    See issue bpo-7098 for a discussion.

    I propose to close this issue.

    @py-user
    Copy link
    Mannequin Author

    py-user mannequin commented Jan 22, 2012

    my question is about the "#" option
    it is described as working with Decimal but it doesn't work with Decimal

    @mdickinson
    Copy link
    Member

    IMO, the behaviour is fine; it's the docs that are unclear. The rules for Decimal are different mainly because trailing zeros have meaning for the Decimal type. (Decimal('1.250') and Decimal('1.25') are two distinct Decimal objects, unlike float('1.250') and float('1.25').)

    See also issue bpo-7094.

    @mdickinson
    Copy link
    Member

    Ah no, I take it back. I think (2) is fine---this is the usual preservation of trailing zeros where possible. (1) needs to be fixed (and issue bpo-7094 was left open waiting for this fix).

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants