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

Implement __format__ for Decimal #46364

Closed
facundobatista opened this issue Feb 14, 2008 · 13 comments
Closed

Implement __format__ for Decimal #46364

facundobatista opened this issue Feb 14, 2008 · 13 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@facundobatista
Copy link
Member

BPO 2110
Nosy @rhettinger, @facundobatista, @mdickinson, @ericvsmith
Files
  • decimal_format.patch
  • decimal_n_format.patch: Implement 'n' format specifier for Decimals
  • decimal_n_format2.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 2009-03-17.23:16:18.105>
    created_at = <Date 2008-02-14.13:04:14.680>
    labels = ['type-bug', 'library']
    title = 'Implement __format__ for Decimal'
    updated_at = <Date 2009-03-17.23:16:18.104>
    user = 'https://github.com/facundobatista'

    bugs.python.org fields:

    activity = <Date 2009-03-17.23:16:18.104>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2009-03-17.23:16:18.105>
    closer = 'mark.dickinson'
    components = ['Library (Lib)']
    creation = <Date 2008-02-14.13:04:14.680>
    creator = 'facundobatista'
    dependencies = []
    files = ['9539', '13324', '13353']
    hgrepos = []
    issue_num = 2110
    keywords = ['patch']
    message_count = 13.0
    messages = ['62389', '62418', '62461', '62916', '63119', '68447', '83504', '83537', '83538', '83663', '83697', '83699', '83710']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'facundobatista', 'mark.dickinson', 'eric.smith']
    pr_nums = []
    priority = 'critical'
    resolution = 'accepted'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue2110'
    versions = ['Python 3.1', 'Python 2.7']

    @facundobatista
    Copy link
    Member Author

    A remainder.

    @facundobatista facundobatista self-assigned this Feb 14, 2008
    @facundobatista facundobatista added the stdlib Python modules in the Lib dir label Feb 14, 2008
    @mdickinson
    Copy link
    Member

    I can take a look at this if you like. But I don't want to spoil your fun
    :)

    @facundobatista
    Copy link
    Member Author

    Please, be my guest!

    Thanks!

    @mdickinson
    Copy link
    Member

    Here's a first attempt at Decimal.__format__; the patch is against the
    trunk, and should be forward ported as usual to 3.0, with obvious minor
    changes related to str/unicode. It still needs some cleanup and some
    more tests, but I'm posting it now in the hope of getting some feedback.

    I'm adding Raymond Hettinger and Eric Smith to the nosy list in case
    they want to comment.

    Some points of interest:

    • I decided to make plain 'e', 'f' and 'g' formats (without an explicit
      precision) do no rounding: they return a string that captures the exact
      value of the Decimal instance (though it can lose information about
      significant zeros). So format(x, 'e') is basically a way to say 'give
      me str(x), but always include an exponent', and format(x, 'f') gives a
      way to print the value without ever including an exponent. format(x,
      'g') is identical to str(x), except that it always uses the character
      'e' for an exponent (instead of using 'e' or 'E' depending on the
      context).

    There are other possible options here (have a default precision; use
    the precision from the context), but this one seemed to make most sense.
    I'd appreciate opinions.

    • the integer format specifiers are not supported. After noticing that
      the integer format specifiers aren't supported for floats either, I
      think this is the right thing to do.

    • the 'n' format specifier is not supported either; it's supposed to
      use the current locale. I can't find any easy way to do this---it seems
      as though the only real option is to manually fix the decimal point
      character, figure out where to place thousands separators, etc. This
      would add quite a lot of not-really-Decimal-related code to decimal.py,
      and I'm reluctant to do that. The right solution probably involves
      writing some support code and putting it somewhere else in the std. lib.

    • on the subject of not-really-Decimal-related code, it would be great
      if the parse_format_specifier and format_align functions (near the end
      of decimal.py) could be moved somewhere else. I'm planning to reuse
      these functions for Fraction.__format__.

    @mdickinson
    Copy link
    Member

    I've committed a reworked version of the patch in r61123.

    @mdickinson
    Copy link
    Member

    Reopening this; I'd like to have a second go at implementing the 'n'
    format specifier for the Decimal type.

    See bpo-2802 for hints about how to go about this.

    @benjaminp benjaminp reopened this Jun 20, 2008
    @mdickinson
    Copy link
    Member

    Adding support for the 'n' format specifier should be done before 3.1 goes
    out.

    @mdickinson
    Copy link
    Member

    Here's a patch to implement the 'n' format specifier for Decimals
    (see also bpo-5481).

    Raymond, could you give this a sanity check?

    @mdickinson mdickinson assigned rhettinger and unassigned mdickinson Mar 13, 2009
    @rhettinger
    Copy link
    Contributor

    Sure, I will take a look.

    @rhettinger
    Copy link
    Contributor

    Mark, this looks fine.

    Can you add support for PEP-378?

    @rhettinger rhettinger assigned mdickinson and unassigned rhettinger Mar 17, 2009
    @mdickinson
    Copy link
    Member

    New version of decimal_n_format.patch, with support for the thousands
    separator (PEP-378). As discussed on python-dev, during zero-padding the
    patched code adds an extra '0' on the left to avoid a leading ',' if
    necessary. For example:

    >>> format(Decimal('123456'), '08,')
    '0,123,456'

    The Decimal.__format__ method (and support code) had to be fairly
    significantly reworked. However, I'm reasonably confident that this code
    is correct: a patch review would be welcome if anyone has the time;
    otherwise I'll commit this in a couple of days or so.

    @mdickinson mdickinson added the type-bug An unexpected behavior, bug, or error label Mar 17, 2009
    @rhettinger
    Copy link
    Contributor

    The tests you submitted are reassuring. I think you should go ahead and
    commit this.

    @mdickinson
    Copy link
    Member

    Committed, r70439 and r70440.

    @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
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants