473,695 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.setloc ale(locale.LC_A LL, 'de_AT.utf8')
>>locale.locale conv()
{'mon_decimal_p oint': ',', 'int_frac_digit s': 2, 'p_sep_by_space ': 1,
'frac_digits': 2, 'thousands_sep' : '', 'n_sign_posn': 1,
'decimal_point' : ',', 'int_curr_symbo l': 'EUR ', 'n_cs_precedes' : 1,
'p_sign_posn': 1, 'mon_thousands_ sep': ' ', 'negative_sign' : '-',
'currency_symbo l': '\xe2\x82\xac', 'n_sep_by_space ': 1,
'mon_grouping': [3, 3, 0], 'p_cs_precedes' : 1, 'positive_sign' : '',
'grouping': []}
>>locale.curren cy(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.setloc ale(locale.LC_A LL, 'de_DE.utf8')
>>locale.locale conv()
{'mon_decimal_p oint': ',', 'int_frac_digit s': 2, 'p_sep_by_space ': 1,
'frac_digits': 2, 'thousands_sep' : '.', 'n_sign_posn': 1,
'decimal_point' : ',', 'int_curr_symbo l': 'EUR ', 'n_cs_precedes' : 0,
'p_sign_posn': 1, 'mon_thousands_ sep': '.', 'negative_sign' : '-',
'currency_symbo l': '\xe2\x82\xac', 'n_sep_by_space ': 1, 'mon_grouping':
[3, 3, 0], 'p_cs_precedes' : 0, 'positive_sign' : '', 'grouping': [3, 3,
>>locale.curren cy(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.setloc ale(locale.LC_A LL, '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 1529

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 unable to display any Russian text. I've managed to resolve the issue by inserting the following two lines at the start of the application:
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 possible. I think it should work with Unicode now too. Any suggestions in making it faster will be appreciated. Any minor improvements to wording, spelling, etc.. are also welcome.
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 think this is also the case of other languages/countries. If I am correct, i.e. if there is no such possibility, where can I file a request for a csv change? Excel, for example, automatically converts '.' to ',' and the separator from ',' to ';'.
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 problem. I used the code below: var_char_copied = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING, var_SYSTEM_DTG, Len(var_SYSTEM_DTG))
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 available here: http://downloads.sourceforge.net/python-sybase/python-sybase-0.39.tar.gz
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... Did you do something to add wx to your PYTHONPATH? Maybe do "import sys; print sys.path" or "import os; print os.environ" to verify this. (Hopefully someone more knowledgeable than me will come along soon.)
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 Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.