Re: Rough draft: Proposed format specifier for a thousands separator



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'

.



Relevant Pages

  • Re: Comma vs. Decimal
    ... time (input in 6 text boxes), and some other numeric data, which can be ... I need to override the comma with a decimal. ... resetting the German computer to the USA locale is ... It corrects some things (allowing decimals in ...
    (microsoft.public.vb.general.discussion)
  • Re: Comma vs. Decimal
    ... In Germany, for instance, the ... I need to override the comma with a decimal. ... resetting the German computer to the USA locale is ... It corrects some things (allowing decimals in ...
    (microsoft.public.vb.general.discussion)
  • Number Formatting Question
    ... How can I customize number formatting so that when I select "Number" I ... don't have decimals and do have a comma? ...
    (microsoft.public.excel)
  • Re: Formatting Numbers
    ... depends on locale, and as far as i could tell ruby does not provide any ... american formatting, and as soon as you choose to change your formatting and ... I would like to output it with a comma, such as "1,000". ...
    (comp.lang.ruby)
  • Re: Problem with reading an int with operator>>
    ... int n, m; ... if the two ints are separated by a comma. ... with the subject locale and streams on VC8 ... so that I dont use the precompiled dll? ...
    (microsoft.public.vc.stl)