Last active
February 28, 2025 16:24
-
-
Save aluminiumgeek/296b2232761cd6f8f9f37665508f55dc to your computer and use it in GitHub Desktop.
Number to Georgian (Amount/price to Georgian)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def number_to_georgian(amount, currency): | |
| if currency == 'GEL': | |
| main_unit = 'ლარი' | |
| fractional_unit = 'თეთრი' | |
| elif currency == 'USD': | |
| main_unit = 'დოლარი' | |
| fractional_unit = 'ცენტი' | |
| elif currency == 'EUR': | |
| main_unit = 'ევრო' | |
| fractional_unit = 'ცენტი' | |
| else: | |
| # Default to GEL if unknown currency | |
| main_unit = 'ლარი' | |
| fractional_unit = 'თეთრი' | |
| # Split into integer and fractional parts | |
| integer_part = int(amount) | |
| fractional_amount = amount - integer_part | |
| fractional_part = round(fractional_amount * 100) | |
| # Handle cases where rounding makes fractional_part 100 or negative | |
| if fractional_part >= 100: | |
| integer_part += 1 | |
| fractional_part -= 100 | |
| elif fractional_part < 0: | |
| fractional_part = 0 | |
| # Helper functions for converting numbers to text | |
| units = { | |
| 0: '', 1: 'ერთი', 2: 'ორი', 3: 'სამი', 4: 'ოთხი', 5: 'ხუთი', | |
| 6: 'ექვსი', 7: 'შვიდი', 8: 'რვა', 9: 'ცხრა', 10: 'ათი', | |
| 11: 'თერთმეტი', 12: 'თორმეტი', 13: 'ცამეტი', 14: 'თოთხმეტი', | |
| 15: 'თხუთმეტი', 16: 'თექვსმეტი', 17: 'ჩვიდმეტი', 18: 'თვრამეტი', | |
| 19: 'ცხრამეტი' | |
| } | |
| tens = { | |
| 10: 'ათი', 20: 'ოცი', 30: 'ოცდაათი', 40: 'ორმოცი', 50: 'ორმოცდაათი', | |
| 60: 'სამოცი', 70: 'სამოცდაათი', 80: 'ოთხმოცი', 90: 'ოთხმოცდაათი' | |
| } | |
| hundreds = { | |
| 100: 'ასი', 200: 'ორასი', 300: 'სამასი', 400: 'ოთხასი', | |
| 500: 'ხუთასი', 600: 'ექვსასი', 700: 'შვიდასი', 800: 'რვაასი', | |
| 900: 'ცხრაასი' | |
| } | |
| scales = {1: 'ათას', 2: 'მილიონ', 3: 'მილიარდ'} | |
| def convert_less_than_100(n): | |
| if n == 0: | |
| return '' | |
| if n in units: | |
| return units[n] | |
| elif n in tens: | |
| return tens[n] | |
| else: | |
| for base in reversed([20, 40, 60, 80]): | |
| if base <= n: | |
| remainder = n - base | |
| base_word = tens[base].rstrip('ი') | |
| remainder_word = convert_less_than_100(remainder) | |
| return f"{base_word}და{remainder_word}" | |
| return '' | |
| def convert_less_than_1000(n): | |
| if n < 100: | |
| return convert_less_than_100(n) | |
| hundred_part = (n // 100) * 100 | |
| remainder = n % 100 | |
| hundred_word = hundreds[hundred_part] | |
| if remainder == 0: | |
| return hundred_word | |
| else: | |
| hundred_word = hundred_word.rstrip('ი') | |
| remainder_word = convert_less_than_100(remainder) | |
| return f"{hundred_word} {remainder_word}" if remainder_word else hundred_word | |
| def convert_integer(n): | |
| if n == 0: | |
| return '' | |
| groups = [] | |
| while n > 0: | |
| groups.append(n % 1000) | |
| n = n // 1000 | |
| parts = [] | |
| for i in range(len(groups)-1, -1, -1): | |
| group_value = groups[i] | |
| if group_value == 0: | |
| continue | |
| is_last = True | |
| for j in range(i-1, -1, -1): | |
| if groups[j] != 0: | |
| is_last = False | |
| break | |
| if i == 0: | |
| part = convert_less_than_1000(group_value) | |
| parts.append(part) | |
| else: | |
| scale_word = scales.get(i, '') | |
| if not scale_word: | |
| continue | |
| if group_value == 1: | |
| part = scale_word | |
| if is_last: | |
| part += 'ი' | |
| else: | |
| group_text = convert_less_than_1000(group_value) | |
| part = f"{group_text} {scale_word}" | |
| if is_last: | |
| part += 'ი' | |
| parts.append(part) | |
| return ' '.join(parts).strip() | |
| # Convert integer part | |
| integer_text = convert_integer(integer_part) | |
| if not integer_text: | |
| integer_text = 'ნული' | |
| main_part = f"{integer_text} {main_unit}" | |
| # Convert fractional part if present | |
| fractional_text = '' | |
| if fractional_part > 0: | |
| fractional_text = convert_integer(fractional_part) | |
| if not fractional_text: | |
| fractional_text = 'ნული' | |
| fractional_text = f"{fractional_text} {fractional_unit}" | |
| # Combine the parts | |
| if fractional_part > 0: | |
| return f"{main_part}, {fractional_text}" | |
| else: | |
| return main_part |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment