4

I want to format price in integer to properly formatted currency. Example 10000 to or ₹10,000

So, I am using the following commands in python

import locale
locale.setlocale(locale.LC_MONETARY, 'en_US')
or
locale.setlocale(locale.LC_MONETARY, 'en_IN')
print str(locale.currency(10000, grouping=True))

When I use the above commands in python in ubuntu in different laptop, they are working perfectly fine. But, on windows they are not working.

Its giving me error as follows

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\locale.py", line 581, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

How to solve this error ?

I am using Windows 10. I open cmd and type "python" enter. The python shell is presented with following version. There I type the above commands.

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32

Note :- I tried copying the locale.py file from python directory in ubuntu system to Windows directory i.e., "C:\Python27\Lib" but its still not working.

Avtar Singh
  • 261
  • 8
  • 14
  • Please follow this: [locale settings](https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting) – Dharmesh Fumakiya Aug 31 '17 at 18:01
  • @Dharmesh the link you provided gives answers for ubuntu system, and not for windows system. Have you tried any solution ? If yes, then please give the solution. Thank You. – Avtar Singh Aug 31 '17 at 18:06
  • Also look here: [What is the correct way to set Python's locale on Windows?](https://stackoverflow.com/questions/955986/what-is-the-correct-way-to-set-pythons-locale-on-windows). – GIZ Aug 31 '17 at 18:23
  • @direprobs I have seen that link. and I can set 'en_US' for $ but I need 'en_IN' for ₹ How can I do that ? Just try it please. – Avtar Singh Aug 31 '17 at 18:29

3 Answers3

2

You can take a look at the pycountry library to have a mapping between Windows and Linux language codes:

>>> pycountry.languages.lookup('fr')
Language(alpha_2=u'fr', alpha_3=u'fra', bibliographic=u'fre', name=u'French', scope=u'I', type=u'L')
>>> pycountry.languages.lookup('french')
Language(alpha_2=u'fr', alpha_3=u'fra', bibliographic=u'fre', name=u'French', scope=u'I', type=u'L')
>>> pycountry.languages.lookup('chinese')
Language(alpha_2=u'zh', alpha_3=u'zho', bibliographic=u'chi', name=u'Chinese', scope=u'M', type=u'L')
>>> pycountry.languages.lookup('chinese-traditional')
Traceback (most recent call last):
  ...
LookupError: Could not find a record for 'chinese-traditional'

Then you can do:

import os
import locale
import pycountry

lang = "en_IN"  # your code
language = pycountry.languages.lookup(lang)
if os.name == "posix":
    locale.setlocale(locale.LC_MONETARY, language.alpha_2)
else:
    locale.setlocale(locale.LC_MONETARY, language.name)

EDIT

To format currency values, you may consider using Babel, for instance:

>>> babel.numbers.format_currency(10000, 'INR', locale='en_IN')
u'\u20b9\xa010,000.00'

>>> print(babel.numbers.format_currency(10000, 'INR', locale='en_IN'))
₹ 10,000.00
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • its working, but there is one slight issue for which I want to know the reason. I used the above code and used lang = "hindi" then after setlocale in else part, it was success but when I used the following line print str(locale.currency(10000, grouping=True)) it gives me the following output ?10,000.00 I want to know why there is ? instead of ₹ any Idea ? or have I done anything wrong ? – Avtar Singh Aug 31 '17 at 19:27
  • str() uses 7-bit ASCII (in Python 2.7) for conversion from unicode, your currency symbol is outside of that range, so it gets converted to the replacement char. – schlenk Aug 31 '17 at 20:01
  • Is there any solution for that ? without str also its question-mark – Avtar Singh Aug 31 '17 at 20:07
  • @AvtarSingh: You may consider using [Babel](http://babel.pocoo.org/en/latest/api/numbers.html). – Laurent LAPORTE Aug 31 '17 at 21:04
  • On Windows, the `en-IN` locale is available for Windows Server 2008 and Windows Vista, Windows 7, Windows Server 2008 R2, Windows 8, Windows Server 2012, Windows 8.1, and Windows Server 2012 R2. Not for Windows 10. See: https://msdn.microsoft.com/en-us/library/cc233982.aspx – Laurent LAPORTE Aug 31 '17 at 21:19
  • @LaurentLAPORTE ohh.....thats the reason behind all this.....then I think we can not do anything regarding this matter.....I'll try once in windows-7 and see what happens and then I will revert back to you......I will also give a shot to Babel.....Thank You – Avtar Singh Sep 01 '17 at 02:21
  • @LaurentLAPORTE I still have not tried on Windows-7 but I just tried Babel on Windows-10 and its nice....but its giving me the following error........ UnicodeEncodeError: 'latin-1' codec can't encode character u'\u20b9' in position 0: ordinal not in range(256).....when I use following command......print format_currency(1099.98, 'INR', locale='en_IN')......for USD and en_US.....its printing $ perfectly......how u printed ₹ ? – Avtar Singh Sep 01 '17 at 05:53
  • 1
    @AvtarSingh: You have this error because your console doesn't support UTF-8 and is configured to use ISO-8859-1 (latin1). So, this is only a problem with `print` on your console, the result is OK. – Laurent LAPORTE Sep 01 '17 at 07:39
  • 1
    "en-IN" is available in the Windows NLS API starting with Vista, and it's certainly available in Windows 10. However, the C runtime that's used by Python 2.7 doesn't understand the new WinAPI locale-name strings that were introduced with Vista, such as "en-IN". It uses LCIDs instead, which are looked up via either the full names or 3-letter abbreviations (e.g. "english_india" or "eng_ind"). Even in Python 3, the CRT version handles locale-name strings, but the locale module itself doesn't, primarily because it wants to use underscore and Windows requires a hyphen. – Eryk Sun Sep 01 '17 at 18:06
  • 1
    The issue with "?" for the currency symbol is because the "en-IN" locale uses 1252 as its legacy codepage, which is meant for Western Europe and doesn't map the "₹" character. The C runtime used by Python 2.7 (VS 2008) doesn't have wide-character alternatives for these fields in the `lconv` struct that C `localeconv` returns. Starting with VS 2010 (used by Python 3.4), MSVC added `wchar_t` alternatives such as `_W_currency_symbol`, so this wart could be addressed in Python 3 if you want to create an issue on the tracker. – Eryk Sun Sep 01 '17 at 18:18
  • @eryksun thank you very much for the info......directly using the following command without any 3rd party library is working absolutely fine...... locale.setlocale(locale.LC_MONETARY, 'eng_ind')......i.e., using "eng_ind" is working fine......but question mark still remains there instead of ₹....but that you also explained why......thanks for all the info – Avtar Singh Sep 02 '17 at 09:35
1

For Python 3.6.4 I did the following on Windows 10

import locale
locale.setlocale(locale.LC_ALL,'enn')
>>'English_India.1252'
locale.currency(10000000.32,grouping=True,symbol=True)
>>'? 1,00,00,000.32'
locale.currency(10000000.32,grouping=True,symbol=True).replace('?','₹')
>>'₹ 1,00,00,000.32'
0

For windows, you need to set locale.setlocale(locale.LC_ALL, '<language string>') https://msdn.microsoft.com/en-us/library/39cwe7zf(vs.71).aspx (deprecated) https://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.140).aspx (updated).

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17
  • You are right for $ i.e., en_US. But how can I set India for (₹) The msdn link does not contain India English language string. I want to use ₹ – Avtar Singh Aug 31 '17 at 18:28
  • I can use 'en_IN' in ubuntu system but I am not able to use it in Windows system. What is 'en_IN' in windows ? Just try it please. – Avtar Singh Aug 31 '17 at 18:31