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

Łukasz Stelmach steelman at post.pl
Sat Jan 5 15:03:06 EST 2019


Barry Scott <barry at barrys-emacs.org> writes:
> On Friday, 4 January 2019 14:57:53 GMT Łukasz Stelmach wrote:
>>
>> 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.
>> 
>> The only format using decimal separator from locale data for
>> float/complex/decimal numbers at the moment is "n" which behaves like
>> "g". The drawback of these formats, I would like to overcome, is the
>> inability to print numbers ranging more than one order of magnitude with
>> the same number of decimal digits without "manually" (with some additional
>> custom code) adjusting precission. The other option is to "manually"
>> replace "." as printed by "f" with a local decimal separator. Neither of
>> these option is appealing to my.
>> 
>> 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 |
>
> Can you use locale.format_string() to solve this?

I am afraid I can't. I am using a library called pint[1] in my
project. It allows me to choose how its objects are formated but it uses
format() internally. It adds some custom extensions to format strings
which, as far as I can tell, mekes it hard if not impossible to patch it
to locale.format_string(). But this is rather an excuse.

I thnik, had this problem some time ago and I got away with
locale.format_string() then, but honestly I think
format()/string.format/__format__ shuld support locale aware "f" just
like there is "n" that behaves like "g".

[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/352233b6/attachment.sig>


More information about the Python-ideas mailing list