View Code Snippet [PHP]

Get amount in words

                                function amount_in_words($amount, $currency = ['currency' => 'Indian Rupees', 'hundreds_name' => 'Paise'])
{
    if ($amount < 0 || $amount > 999999999999)
        return "";

    $dec = 2; // set currency hundreds name

    if ($dec > 0)
    {
        $divisor = pow(10, $dec);
        $frac = round($amount - floor($amount), $dec) * $divisor;
        $frac = sprintf("%0{$dec}d", round($frac, 0));
        $and = " and ";
        $frac = $frac !== '00' ? ' and ' . get_number_to_words(intval($frac)) . ' ' . $currency['hundreds_name'] : '';
    }
    else
    {
        $frac = "";
    }

    $words = get_number_to_words(intval($amount));

    if ($words === "Zero")
    {
        return $words . $frac;
    }
    else
    {
        return $words . ' ' . $currency['currency'] . $frac;
    }
}

function get_number_to_words($number)
{
    // Array containing words for numbers up to nineteen
    $ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];

    // Array containing words for tens multiples
    $tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];

    $res = "";

    if ($number >= 10000000)
    {
        $res .= get_number_to_words(floor($number / 10000000)) . ' Crore ';
        $number %= 10000000;
    }

    if ($number >= 100000)
    {
        $res .= get_number_to_words(floor($number / 100000)) . ' Lahk ';
        $number %= 100000;
    }

    if ($number >= 1000)
    {
        $res .= get_number_to_words(floor($number / 1000)) . ' Thousand ';
        $number %= 1000;
    }

    if ($number >= 100)
    {
        $res .= get_number_to_words(floor($number / 100)) . ' Hundred ';
        $number %= 100;
    }

    if ($number > 0)
    {
        if ($number < 20)
        {
            $res .= $ones[$number];
        }
        else
        {
            $res .= $tens[floor($number / 10)];
            if (($number % 10) > 0)
            {
                $res .= ' ' . $ones[$number % 10];
            }
        }
    }

    return $res;
}

// Usage
echo amount_in_words(200123602.45);

//output
Twenty Crore One Lahk Twenty Three Thousand Six Hundred Two Indian Rupees and Forty Five Paise