Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python locale.localeconv() Function



The Python locale.localeconv() function is used to retrieve the current locale conventions as a dictionary. This dictionary contains information related to numeric formatting and currency settings for the currently set locale.

This function is particularly useful for applications that need to format numbers and currency according to regional settings.

Syntax

Following is the syntax of the Python locale.localeconv() function −

locale.localeconv()

Parameters

This function does not take any parameters.

Return Value

This function returns a dictionary containing the locale’s numeric and monetary formatting settings.

Example 1

Following is an example of the Python locale.localeconv() function. Here, we retrieve the locale settings for the default locale −

import locale

locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
locale_info = locale.localeconv()
print(locale_info)

Following is the output of the above code (output may vary depending on the system) −

{'int_curr_symbol': 'USD', 'currency_symbol': '$', 'mon_decimal_point': '.', 'mon_thousands_sep': ',', 'mon_grouping': [3, 0], 'positive_sign': '', 'negative_sign': '-', 'int_frac_digits': 2, 'frac_digits': 2, 'p_cs_precedes': 1, 'p_sep_by_space': 0, 'n_cs_precedes': 1, 'n_sep_by_space': 0, 'p_sign_posn': 3, 'n_sign_posn': 0, 'decimal_point': '.', 'thousands_sep': ',', 'grouping': [3, 0]}

Example 2

Here, we change the locale to French and retrieve the locale conventions −

import locale

locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
locale_info = locale.localeconv()
print(locale_info)

Output of the above code is as follows (output may vary based on system settings) −

{'int_curr_symbol': 'EUR', 'currency_symbol': '€', 'mon_decimal_point': ',', 'mon_thousands_sep': '\u202f', 'mon_grouping': [3, 0], 'positive_sign': '', 'negative_sign': '-', 'int_frac_digits': 2, 'frac_digits': 2, 'p_cs_precedes': 0, 'p_sep_by_space': 1, 'n_cs_precedes': 0, 'n_sep_by_space': 1, 'p_sign_posn': 1, 'n_sign_posn': 1, 'decimal_point': ',', 'thousands_sep': '\u202f', 'grouping': [3, 0]}

Example 3

Now, we use the locale.localeconv() function to extract specific information about currency formatting −

import locale

locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
locale_info = locale.localeconv()
currency_symbol = locale_info['currency_symbol']
decimal_point = locale_info['decimal_point']
print("Currency Symbol:", currency_symbol)
print("Decimal Point Character:", decimal_point)

The result obtained is as shown below (output may vary) −

Currency Symbol: €
Decimal Point Character: ,

Example 4

We can use the locale.localeconv() function to format a currency string manually using the locale settings −

import locale

locale.setlocale(locale.LC_ALL, "it_IT.UTF-8")
locale_info = locale.localeconv()
currency_symbol = locale_info['currency_symbol']
thousands_sep = locale_info['thousands_sep']
amount = "1{}234{}56".format(thousands_sep, locale_info['decimal_point'])
formatted_currency = currency_symbol + " " + amount
print("Formatted Currency:", formatted_currency)

The result produced is as follows (output may vary) −

Formatted Currency: € 1.234,56
python_modules.htm
Advertisements