[Python-ideas] Fixed point format for numbers with locale based separators

Łukasz Stelmach steelman at post.pl
Sat Jan 5 10:41:20 EST 2019


Steven D'Aprano <steve at pearwood.info> writes:

> On Fri, Jan 04, 2019 at 03:57:53PM +0100, Łukasz Stelmach wrote:
>> Hi,
>> 
>> I would like to present two pull requests[1][2] implementing fixed point
>> presentation of numbers and ask for comments. The first is mine. I
>> learnt about the second after publishing mine.
>
> Before I look at the implementation, can you explain the functional 
> requirements please?

As I stated in the original message below the table:

>> In the application I want to create I am going to present users numbers
>> ranging up to 3 orders of magnitude and I (my users) want them to be
>> presented consistently with regards to number of decimal digits AND I
>> want to conform to rules of languages of my users. And I would like to
>> avoid the exponent notation by all means.

The pint[1] library I use, implements formatting of physical quantities
using the format()/__format__ code. As far as I can tell my patch for
Python is shorter and more straightforward than a patch for pint to use
locale.format().

Because the "g" based "n" formatter has been present since the advanced
string formatting was described in PEP-3101, I think it is necessary to
add the "m" formatter based on "f". The advanced string formatting
facility in Python is very convenient and programmers shouldn't forced
to use locale.format() like this

    "The total length of {} sticks is {} meters.".format(n_sticks, locale.format(".2f", l_sticks))

instead of

    "The total length of {} sticks is {:.2f} meters.".format(n_sticks, l_sticks)

> In other words, what is the new feature you hope to have excepted? 
> Explain the intention and the API (the interface). The implementation is 
> the least important part :-)

I wish to add a new formatter "m" for float/complex/decimal numbers,
which behaves like the existing "f", but uses the decimal separator from
the locale database. There is "n" formmatter which behaves like "g" but
it does not fit my needs.

> [...]
>> Formatting 1.23456789 * n (LC_ALL=3Dpl_PL.UTF-8)
>>     | n |    ".2f" |    ".3n" |
>>     |---+----------+----------|
>>     | 1 |     1.23 |     1,23 |
>>     | 2 |    12.35 |     12,3 |
>>     | 3 |   123.46 |      123 |
>>     | 4 |  1234.57 | 1,23e+03 |
>
> I'm afraid I cannot work out what that table means. You say "Formatting 
> 1.23... * n" (multiplying by n) but the results shown aren't multiplied 
> by n=2, n=3, n=4 as the table suggests.
>
> Can you show what Python code you expect will produce the expected 
> output?

    for n in range(1,5): print("| {} | {:8.2f} | {:8.3n} |".format(n,1.23456789 * 10**n, 1.23456789 * 10**n))

[1] http://pint.readthedocs.io/
-- 
Było mi bardzo miło.                                  --- Rurku. --- ...
>Łukasz<                                --- To dobrze, że mnie słuchasz.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 617 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190105/9a2fc15d/attachment.sig>


More information about the Python-ideas mailing list