Rough draft: Proposed format specifier for a thousands separator

MRAB google at mrabarnett.plus.com
Thu Mar 12 14:15:19 CET 2009


Raymond Hettinger wrote:
[snip]
> Proposal I (from Nick Coghlan):
> -------------------------------
> 
> A comma will be added to the format() specifier mini-language:
> 
> [[fill]align][sign][#][0][minimumwidth][,][.precision][type]
> 
> The ',' option indicates that commas should be included in the
> output as a thousands separator. As with locales which do not
> use a period as the decimal point, locales which use a
> different convention for digit separation will need to use the
> locale module to obtain appropriate formatting.
> 
> The proposal works well with floats, ints, and decimals.
> It also allows easy substitution for other separators.
> For example:
> 
>   format(n, "6,f").replace(",", "_")
> 
> This technique is completely general but it is awkward in the
> one case where the commas and periods need to be swapped:
> 
>   format(n, "6,f").replace(",", "X").replace(".", ",").replace("X",
> ".")
> 
> 
> Proposal II (to meet Antoine Pitrou's request):
> -----------------------------------------------
> 
> Make both the thousands separator and decimal separator user
> specifiable but not locale aware.  For simplicity, limit the
> choices to a comma, period, space, or underscore.
> 
> [[fill]align][sign][#][0][minimumwidth][T[tsep]][dsep precision][type]
> 
> Examples:
> 
>   format(1234, "8.1f")    -->     '  1234.0'
>   format(1234, "8,1f")    -->     '  1234,0'
>   format(1234, "8T.,1f")  -->     ' 1.234,0'
>   format(1234, "8T .f")   -->     ' 1 234,0'
>   format(1234, "8d")      -->     '    1234'
>   format(1234, "8T,d")    -->     '   1,234'
> 
> This proposal meets mosts needs (except for people wanting
> grouping for hundreds or ten-thousands), but iIt comes at the
> expense of being a little more complicated to learn and
> remember.  Also, it makes it more challenging to write custom
> __format__ methods that follow the format specification
> mini-language.
> 
> For the locale module, just the "T" is necessary in a
> formatting string since the tool already has procedures for
> figuring out the actual separators from the local context.
> 
[snip]
I'd probably prefer Proposal I with "." representing the decimal point
and "," representing the grouping (thousands) separator, although I'd
add an "L" flag to indicate that it should use the locale to provide the
actual characters to be used and even the number of digits for the
grouping:

[[fill]align][sign][#][0][minimumwidth][,][.precision][L][type]

Examples:

   Assuming the locale has:

     decimal point:      ","
     grouping separator: "."
     grouping spacing:   3

   format(123456, "10.1f")    -->     '  123456.0'
   format(123456, "10.1Lf")   -->     ' 123.456,0'
   format(123456, "10,.1f")   -->     ' 123,456.0'
   format(123456, "10,.1Lf")  -->     ' 123.456,0'




More information about the Python-list mailing list