Python locale.atoi() Function
The Python locale.atoi() function is used to convert a locale-formatted string representing an integer into an actual integer. This function is particularly useful when dealing with numbers that use locale-specific formatting, such as thousands separators.
It ensures that numbers formatted according to locale settings can be correctly interpreted and used in numerical computations.
Syntax
Following is the syntax of the Python locale.atoi() function −
locale.atoi(val)
Parameters
This function accepts a locale-formatted string as a parameter representing an integer.
Return Value
This function returns an integer representing the parsed number.
Example 1
Following is an example of the Python locale.atoi() function. Here, we convert a locale-formatted string into an integer −
import locale
locale.setlocale(locale.LC_ALL, '')
localized_number = "1,234,567"
converted_number = locale.atoi(localized_number)
print("Localized Number:", localized_number)
print("Converted Integer:", converted_number)
Following is the output of the above code (output may vary depending on the system locale) −
Localized Number: 1,234,567 Converted Integer: 1234567
Example 2
Here, we demonstrate how locale.atoi() function can handle different locale formats −
import locale
def convert_locale_number(value, loc):
try:
# Use Windows-compatible locale
locale.setlocale(locale.LC_ALL, loc)
conv = locale.localeconv() # Get locale-specific formatting rules
# Print locale details for debugging
print(f"Locale ({loc}) - Thousands Sep: '{conv['thousands_sep']}', Decimal Sep: '{conv['decimal_point']}'")
# Remove thousands separator dynamically
thousands_sep = conv['thousands_sep']
if thousands_sep:
value = value.replace(thousands_sep, '')
# Also remove any non-breaking spaces (French uses them)
value = value.replace('\xa0', '').replace('\u00A0', '')
# Convert to integer
converted_number = int(value)
print(f"Converted Integer: {converted_number}")
except locale.Error:
print(f"Locale '{loc}' is not supported on this system.")
except ValueError as e:
print(f"Error converting '{value}' for locale {loc}: {e}")
# Use system-supported Windows locales
convert_locale_number("98 765", 'French_France.1252') # French
convert_locale_number("98.765", 'German_Germany.1252') # German
Output of the above code is as follows (output may vary based on system settings) −
Locale (French_France.1252) - Thousands Sep: ' ', Decimal Sep: ',' Error converting '98 765' for locale French_France.1252: invalid literal for int() with base 10: '98 765' Locale (German_Germany.1252) - Thousands Sep: '.', Decimal Sep: ',' Converted Integer: 98765
Example 3
Now, we use the locale.atoi() function to process user input dynamically −
import locale
def process_user_input(user_input):
locale.setlocale(locale.LC_ALL, '')
return locale.atoi(user_input)
user_input = "1,234,567"
converted_number = process_user_input(user_input)
print("Processed Numeric Value:", converted_number)
The result obtained is as shown below (output may vary) −
Processed Numeric Value: 1234567
Example 4
We can use the locale.atoi() function to convert user-inputted numbers before performing calculations −
import locale
def calculate_sum(num1, num2):
locale.setlocale(locale.LC_ALL, '')
num1 = locale.atoi(num1)
num2 = locale.atoi(num2)
return num1 + num2
sum_result = calculate_sum("1,500", "2,500")
print("Sum:", sum_result)
The result produced is as follows (output may vary) −
Sum: 4000
python_modules.htm