Skip to content

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

Closed

Description

@mdickinson
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']

    Activity

    1. mdickinson commented on Oct 10, 2009

      @mdickinson
      MemberAuthor

      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.)

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Oct 10, 2009
    3. self-assigned this
      on Oct 10, 2009
    4. mdickinson commented on Oct 10, 2009

      @mdickinson
      MemberAuthor

      Here's a patch for trunk.

    5. mdickinson commented on Oct 10, 2009

      @mdickinson
      MemberAuthor

      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.

    6. added
      docsDocumentation in the Doc dir
      and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Nov 28, 2009
    7. rhettinger commented on Dec 24, 2011

      @rhettinger
      Contributor

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

    8. skrah commented on Jan 4, 2012

      skrahmannequin
      Mannequin

      [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.

    9. mdickinson commented on Oct 28, 2012

      @mdickinson
      MemberAuthor

      Closing as invalid.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    docsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions