Преобразование индийской цены в слова с примерами кода

Чтобы преобразовать индийскую цену в слова с помощью кода, вы можете использовать следующий метод Python:

Метод 1: использование NumPy и библиотеки Inflect

import inflect
import numpy as np
def convert_price_to_words(price):
    # Split price into rupees and paise
    rupees, paise = str(price).split('.')

    # Convert rupees to words
    p = inflect.engine()
    rupees_words = p.number_to_words(int(rupees))

    # Convert paise to words
    paise_words = p.number_to_words(int(paise))

    # Build the result
    result = rupees_words + " rupees"
    if int(paise) > 0:
        result += " and " + paise_words + " paise"

    return result
# Example usage
price = 12345.67
words = convert_price_to_words(price)
print(words)  # Output: "twelve thousand three hundred forty-five rupees and sixty-seven paise"

Метод 2. Использование собственной логики

def convert_price_to_words(price):
    # Split price into rupees and paise
    rupees, paise = str(price).split('.')

    # Define word lists
    units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
    teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']

    # Convert rupees to words
    rupees_words = ''
    if len(rupees) > 5:
        return "Price is too high to convert"
    if int(rupees) > 0:
        if len(rupees) == 5:
            rupees_words += units[int(rupees[0])] + " thousand "
            rupees = rupees[1:]
        if len(rupees) == 4:
            rupees_words += units[int(rupees[0])] + " thousand "
            rupees = rupees[1:]
        if len(rupees) == 3:
            rupees_words += units[int(rupees[0])] + " hundred "
            rupees = rupees[1:]
        if len(rupees) == 2:
            if rupees[0] == '1':
                rupees_words += teens[int(rupees[1])] + " "
                rupees = ''
            else:
                rupees_words += tens[int(rupees[0])] + " "
                rupees = rupees[1:]
        if len(rupees) == 1:
            rupees_words += units[int(rupees[0])] + " "

    # Convert paise to words
    paise_words = units[int(paise)]

    # Build the result
    result = rupees_words + "rupees"
    if int(paise) > 0:
        result += " and " + paise_words + " paise"

    return result
# Example usage
price = 12345.67
words = convert_price_to_words(price)
print(words)  # Output: "twelve thousand three hundred forty-five rupees and sixty-seven paise"