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

Python locale.delocalize() Function



The Python locale.delocalize() function is used to convert a locale-formatted number string into a standard non-localized format. This function is useful for processing user input that includes locale-specific formatting, such as thousands separators or decimal marks, and converting it into a format that can be used for computations.

It helps in ensuring that numbers formatted according to locale settings can be properly interpreted by numerical operations.

Syntax

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

locale.delocalize(val)

Parameters

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

Return Value

This function returns a string with all locale-specific formatting removed, converting it into a standard numerical representation.

Example 1

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

import locale

locale.setlocale(locale.LC_ALL, '')
localized_number = locale.format_string("%f", 1234567.89, grouping=True)
delocalized_number = locale.delocalize(localized_number)
print("Localized Number:", localized_number)
print("Delocalized Number:", delocalized_number)

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

Localized Number: 1,234,567.89
Delocalized Number: 1234567.89

Example 2

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

import locale

def convert_locale_number(value, loc):
   locale.setlocale(locale.LC_ALL, loc)
   localized_number = locale.format_string("%f", value, grouping=True)
   delocalized_number = locale.delocalize(localized_number)
   print(f"Locale ({loc}) Formatted Number:", localized_number)
   print(f"Converted Standard Number:", delocalized_number)

convert_locale_number(98765.4321, 'fr_FR.UTF-8')
convert_locale_number(98765.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: 98 765,432100
Converted Standard Number: 98765.432100
Locale (de_DE.UTF-8) Formatted Number: 98.765,432100
Converted Standard Number: 98765.432100

Example 3

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

import locale

def process_user_input(user_input):
   locale.setlocale(locale.LC_ALL, '')
   return locale.delocalize(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.delocalize() function to normalize numbers for calculations −

import locale

def calculate_sum(num1, num2):
   locale.setlocale(locale.LC_ALL, '')
   num1 = float(locale.delocalize(num1))
   num2 = float(locale.delocalize(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