Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Convert Suffix denomination to Values
When working with financial or numerical data, you often encounter values with suffix denominations like "5Cr" (5 Crores), "7M" (7 Million), or "30K" (30 Thousand). Python allows you to convert these suffix notations to their actual numerical values using dictionaries and string manipulation.
Example
Below is a demonstration of converting suffix denominations to values ?
my_list = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"]
print("The list is :")
print(my_list)
value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000,
"L": 100000, "K": 1000, "Tr": 1000000000000}
my_result = []
for element in my_list:
for key in value_dict:
if key in element:
val = float(element.replace(key, "")) * value_dict[key]
my_result.append(val)
print("The resultant values :")
print(my_result)
The list is : ['5Cr', '7M', '9B', '12L', '20Tr', '30K'] The resultant values : [50000000.0, 7000000.0, 9000000000.0, 1200000.0, 20000000000000.0, 30000.0]
How It Works
The algorithm follows these steps:
- Dictionary Mapping: A dictionary maps suffix abbreviations to their numerical multipliers (K=1000, M=1000000, etc.)
- String Processing: For each element, the code checks if any suffix key exists in the string
-
Value Extraction: The numeric part is extracted by removing the suffix using
replace() - Multiplication: The numeric value is multiplied by the corresponding multiplier from the dictionary
Alternative Approach Using endswith()
A more precise method using endswith() to avoid partial matches ?
def convert_suffix_to_value(text):
value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000,
"L": 100000, "K": 1000, "Tr": 1000000000000}
for suffix, multiplier in value_dict.items():
if text.endswith(suffix):
numeric_part = float(text.replace(suffix, ""))
return numeric_part * multiplier
return float(text) # Return as-is if no suffix found
data = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"]
result = [convert_suffix_to_value(item) for item in data]
print("Original:", data)
print("Converted:", result)
Original: ['5Cr', '7M', '9B', '12L', '20Tr', '30K'] Converted: [50000000.0, 7000000.0, 9000000000.0, 1200000.0, 20000000000000.0, 30000.0]
Suffix Denomination Reference
| Suffix | Full Form | Multiplier | Example |
|---|---|---|---|
| K | Thousand | 1,000 | 30K = 30,000 |
| L | Lakh | 100,000 | 12L = 1,200,000 |
| M | Million | 1,000,000 | 7M = 7,000,000 |
| Cr | Crore | 10,000,000 | 5Cr = 50,000,000 |
| B | Billion | 1,000,000,000 | 9B = 9,000,000,000 |
| Tr | Trillion | 1,000,000,000,000 | 20Tr = 20,000,000,000,000 |
Conclusion
Converting suffix denominations to numerical values is straightforward using dictionary mapping and string manipulation. The endswith() approach provides more accuracy by avoiding partial matches, making it suitable for processing financial data or large datasets with abbreviated number formats.
