472,377 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,377 software developers and data experts.

grouping in module 'locale'


I try to format monetary values using the locale module, python2.5:

Python 2.5.2a0 (r251:54863, Jan 3 2008, 17:59:56)
[GCC 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import locale
locale.setlocale(locale.LC_ALL, 'de_AT.utf8')
{'mon_decimal_point': ',', 'int_frac_digits': 2, 'p_sep_by_space': 1,
'frac_digits': 2, 'thousands_sep': '', 'n_sign_posn': 1,
'decimal_point': ',', 'int_curr_symbol': 'EUR ', 'n_cs_precedes': 1,
'p_sign_posn': 1, 'mon_thousands_sep': ' ', 'negative_sign': '-',
'currency_symbol': '\xe2\x82\xac', 'n_sep_by_space': 1,
'mon_grouping': [3, 3, 0], 'p_cs_precedes': 1, 'positive_sign': '',
'grouping': []}
>>locale.currency(1234.5678, grouping=True, symbol=False)

As you can see, the decimal point is correctly set to ','. But the
grouping is not done, every 3 digits should be separated by space (' ').
Using the 'de_DE.utf8' locale, ist works:
>>locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
{'mon_decimal_point': ',', 'int_frac_digits': 2, 'p_sep_by_space': 1,
'frac_digits': 2, 'thousands_sep': '.', 'n_sign_posn': 1,
'decimal_point': ',', 'int_curr_symbol': 'EUR ', 'n_cs_precedes': 0,
'p_sign_posn': 1, 'mon_thousands_sep': '.', 'negative_sign': '-',
'currency_symbol': '\xe2\x82\xac', 'n_sep_by_space': 1, 'mon_grouping':
[3, 3, 0], 'p_cs_precedes': 0, 'positive_sign': '', 'grouping': [3, 3,
>>locale.currency(1234.5678, grouping=True, symbol=False)

The difference here is that thounds_sep is '.', not ' '. If we look at the code
of locale.py, revision 55038, lines 157-161, the inserted spaces are later
deleted again:

while seps:
sp = formatted.find(' ')
if sp == -1: break
formatted = formatted[:sp] + formatted[sp+1:]
seps -= 1

This code is only called if numbers are formated as floating point, but not for
integers. The following works:
>>locale.setlocale(locale.LC_ALL, 'de_AT.utf8')
>>locale.format('%d',1234.5678, grouping=True, monetary=True)
'1 234'

The reason for the space removal is explained in an earlier version of
locale.py, e.g. 42120:

# If the number was formatted for a specific width, then it
# might have been filled with spaces to the left or right. If
# so, kill as much spaces as there where separators.
# Leading zeroes as fillers are not yet dealt with, as it is
# not clear how they should interact with grouping.

But I don't know the why and how this code is necessary. Can anybody shed some
light on this issue?

Best Regards, Roman
Jan 25 '08 #1
0 1421

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Paul McGuire | last post by:
.... what will become of the module-level constants, such as uppercase, digits, hexdigits, etc.? Will they become class-level constants of the str class? -- Paul
by: BigMan | last post by:
Is it possible to specify digit grouping when writing to an output stream (so that std::cout << 1234.56789; produces "1 234.567 89", for example)? If so, how?
by: Damien Elmes | last post by:
Hi folks, I've got a module + C extension which provides on screen display support in X, via libxosd (http://repose.cx/pyosd). I've recently had a report of trouble where a Russian user was...
by: Ron Adam | last post by:
I've made a few more changes to my little collate module. There might be better ways to handle the options, or better choices for the options themselves. I tried to keep it as general as...
by: Paulo da Silva | last post by:
Hi. I have just seen that csv module, more exactly the Dialect class, does not have any variable to specify the "floating point" character! In portuguese this is ','. Not '.'. 3.1415 -3,1415. I...
by: Chris | last post by:
I tried to retrieve the digit grouping symbol in MSAccess but unfortunately 3;0 is retrieved instead of comma which is my symbol. Function retrieves decimal symbol and list separator without any...
by: =?ISO-8859-1?Q?S=E9bastien_Sabl=E9?= | last post by:
WHAT IS IT: The Sybase module provides a Python interface to the Sybase relational database system. It supports all of the Python Database API, version 2.0 with extensions. The module is...
by: Matt Nordhoff | last post by:
Barak, Ron wrote: For some reason, when you run "import locale", it's importing wx.locale instead of the "locale" module from the stdlib. However, I have no idea why that would be happening......
by: John [H2O] | last post by:
There's a lot of greek for me here ... should I post to numpy-discussions as well??? The backtrace is at the bottom.... Thanks! GNU gdb Fedora (6.8-21.fc9) Copyright (C) 2008 Free...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.