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

g formatting for decimal types should always strip trailing zeros. #51347

Closed
mdickinson opened this issue Oct 10, 2009 · 6 comments
Closed

g formatting for decimal types should always strip trailing zeros. #51347

mdickinson opened this issue Oct 10, 2009 · 6 comments
Assignees
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@mdickinson
Copy link
Member

BPO 7098
Nosy @rhettinger, @mdickinson, @ericvsmith, @skrah, @py-user
Files
  • issue7098.patch
  • 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 = 'https://github.com/mdickinson'
    closed_at = <Date 2012-10-28.10:29:59.463>
    created_at = <Date 2009-10-10.18:22:10.872>
    labels = ['type-bug', 'invalid', 'docs']
    title = 'g formatting for decimal types should always strip trailing zeros.'
    updated_at = <Date 2012-10-28.10:29:59.462>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2012-10-28.10:29:59.462>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2012-10-28.10:29:59.463>
    closer = 'mark.dickinson'
    components = ['Documentation']
    creation = <Date 2009-10-10.18:22:10.872>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['15097']
    hgrepos = []
    issue_num = 7098
    keywords = ['patch']
    message_count = 6.0
    messages = ['93835', '93839', '93840', '150235', '150629', '174025']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'eric.smith', 'skrah', 'py.user']
    pr_nums = []
    priority = 'low'
    resolution = 'not a bug'
    stage = 'needs patch'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue7098'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    @mdickinson
    Copy link
    Member Author

    Type 'g' formatting for Decimal instances doesn't behave in the same way
    as for floats when an explicit precision is given. It should strip all
    trailing zeros from the result:

    Python 2.7a0 (trunk:75309, Oct 10 2009, 13:44:18) 
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from decimal import Decimal
    >>> format(Decimal('123.00'), '.6g')  # expect '123'
    '123.00'

    (When no explicit precision is given, Decimal formatting with type 'g'
    tries to preserve the information about the exponent of the Decimal
    instance whenever possible; this also differs from float formatting, but
    it's intentional. This should probably be documented.)

    @mdickinson mdickinson added the stdlib Python modules in the Lib dir label Oct 10, 2009
    @mdickinson mdickinson self-assigned this Oct 10, 2009
    @mdickinson mdickinson added the type-bug An unexpected behavior, bug, or error label Oct 10, 2009
    @mdickinson
    Copy link
    Member Author

    Here's a patch for trunk.

    @mdickinson
    Copy link
    Member Author

    It should strip all trailing zeros from the result:

    Hmm. Thinking about this some more, I don't think this is true: format() shouldn't be throwing away significant information (in Decimal
    the number of trailing zeros *is* significant information) unless that's
    necessary to fit the result into the given precision.

    So I think the current code is correct.

    However, we could do with documenting the precise rules being used for
    Decimal formatting somewhere.

    @mdickinson mdickinson added docs Documentation in the Doc dir and removed stdlib Python modules in the Lib dir labels Nov 28, 2009
    @rhettinger
    Copy link
    Contributor

    I would like to think about this one for a bit before it moves forward.

    @skrah
    Copy link
    Mannequin

    skrah mannequin commented Jan 4, 2012

    [Mark]

    So I think the current code is correct.

    I agree with this. Currently the 'g' format is like to_sci_string()
    with the added possibility of adjusting the number of significant
    digits. It's probably hard to come up with a better way to handle
    this for Decimal.

    @mdickinson
    Copy link
    Member Author

    Closing as invalid.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants