Python locale.atof() Function



The Python locale.atof() function is used to convert a locale-formatted string representing a number into a floating-point number. This function is particularly useful when dealing with numbers that use locale-specific formatting, such as commas for decimal points or 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.atof() function −

locale.atof(val)

Parameters

This function accepts a locale-formatted string as a parameter representing a floating-point number.

Return Value

This function returns a float representing the parsed number.

Example 1

Following is an example of the Python locale.atof() function. Here, we convert a locale-formatted string into a floating-point number −

import locale

locale.setlocale(locale.LC_ALL, '')
localized_number = "1,234,567.89"
converted_number = locale.atof(localized_number)
print("Localized Number:", localized_number)
print("Converted Float:", converted_number)

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

Localized Number: 1,234,567.89
Converted Float: 1234567.89

Example 2

Here, we demonstrate how the locale.atof() function can handle different locale formats −

import locale

def convert_locale_number(value, loc):
try:
   locale.setlocale(locale.LC_ALL, loc)
except locale.Error:
   print(f"Locale '{loc}' is not available on your system.")
   return

# Ensure correct thousands separator
if loc.startswith("fr_FR"):
   value = value.replace("\xa0", "").replace(" ", "")

try:
   converted_number = locale.atof(value)
   print(f"Locale ({loc}) Formatted Number: {value}")
   print(f"Converted Float: {converted_number}")
except ValueError as e:
   print(f"Error converting '{value}' in locale '{loc}': {e}")

convert_locale_number("98 765,4321", 'fr_FR.UTF-8')
convert_locale_number("98.765,4321", 'de_DE.UTF-8')

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

Locale (fr_FR.UTF-8) Formatted Number: 98765,4321
Converted Float: 98765.4321
Locale (de_DE.UTF-8) Formatted Number: 98.765,4321
Converted Float: 98765.4321

Example 3

Now, we use the locale.atof() function to process user input dynamically −

import locale

def process_user_input(user_input):
   locale.setlocale(locale.LC_ALL, '')
   return locale.atof(user_input)

user_input = "1,234,567.89"
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.89

Example 4

We can use the locale.atof() function to convert user-inputted numbers before performing calculations −

import locale

def calculate_sum(num1, num2):
   locale.setlocale(locale.LC_ALL, '')
   num1 = locale.atof(num1)
   num2 = locale.atof(num2)
   return num1 + num2

sum_result = calculate_sum("1,500.75", "2,499.25")
print("Sum:", sum_result)

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

Sum: 4000.0
python_modules.htm
Advertisements