[Python-ideas] Format mini-language for lakh and crore

David Mertz mertz at gnosis.cx
Mon Jan 29 10:43:14 EST 2018


Nick suggests:
    >>> print(f"In European format x is {x:,.2f}, in Indian format it
is {x:,2,3.2f}")

This looks very good and general.  I only know of the "European" and South
Asian conventions in widespread use, but we could give other grouping
conventions using that little syntax and it definitely covers the ones I
know about.  There's not an issue about this giving the parser for the
format mini-language hiccups over width specifier in there, is there?


On Mon, Jan 29, 2018 at 2:13 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 29 January 2018 at 11:48, Nathaniel Smith <njs at pobox.com> wrote:
> > On Sun, Jan 28, 2018 at 5:31 PM, David Mertz <mertz at gnosis.cx> wrote:
> >> I actually didn't know about `locale.format("%d", 10e9, grouping=True)`.
> >> But it's still much less general than having the option in the
> >> f-string/.format() mini-language.  This is really about the formatted
> >> string, not necessarily about the locale.  So, e.g. I'd like to be able
> to
> >> write:
> >>
> >>>>> print(f"In European format x is {x:,.2f}, in Indian format it is
> >>>>> {x:`.2f}")
> >>
> >> I don't want the format necessarily to be some pseudo-global setting,
> even
> >> if it can get stored in thread-locals.  That said, having a locale-aware
> >> symbol for delimiting numbers in the format mini-language would also
> not be
> >> a bad thing.
> >
> > I don't understand the format mini-language well enough to know what
> > would fit in, but maybe some way to (a) request localified formatting,
>
> Given the example, I think a more useful approach would be to allow an
> optional digit grouping specifier after the comma separator, and allow
> the separator to be repeated to indicate non-uniform groupings in the
> lower order digits.
>
> If we did that, then David's example could become:
>
>     >>> print(f"In European format x is {x:,.2f}, in Indian format it
> is {x:,2,3.2f}")
>
> The core elements of interpreting that would then be:
>
> - digit group size specifiers are permited for both "," (decimal
> display only) and "_" (all display bases)
> - if no digit group size specifier is given, it defaults to 3 for
> decimal and 4 for binary, octal, and hexadecimal
> - if multiple digit group specifiers are given, then the last one
> given is applied starting from the least significant integer digit
>
> so "{x:,2,3.2f}" means:
>
> - an arbitrary number of leading 2-digit groups
> - 1 group of 3 digits
> - 2 decimal places
>
> It would then be reasonably straightforward to use this as a lower
> level primitive to implement locale dependent formatting, as follows:
>
>     - format in English using the locale's grouping rules [1] (either
> LC_NUMERIC.grouping or LC_MONETARY.mon_grouping, as appropriate)
>     - use str.translate() [2] to replace "," and "." with the locale's
> thousands_sep & decimal_point or mon_thousands_sep & mon_decimal_point
>
> [1] https://docs.python.org/3/library/locale.html#locale.localeconv
> [2] https://docs.python.org/3/library/stdtypes.html#str.translate
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180129/57a1a4c8/attachment-0001.html>


More information about the Python-ideas mailing list