2 years ago

#56087

test-img

Prasanth Pksl

Indian Currency Amount to words function not working in PHP

Please help me to solve this problem. I need to print the invoice in that invoice I need to show the amount in words but the function getting an error. Please help me to solve this problem.

Function:

function getIndianCurrency(float $number)
{
    $decimal = round($number - ($no = floor($number)), 2) * 100;
    $hundred = null;
    $digits_length = strlen($no);
    $i = 0;
    $str = array();
    $words = array(0 => '', 1 => 'one', 2 => 'two',
        3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
        7 => 'seven', 8 => 'eight', 9 => 'nine',
        10 => 'ten', 11 => 'eleven', 12 => 'twelve',
        13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
        16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
        19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
        40 => 'forty', 50 => 'fifty', 60 => 'sixty',
        70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
    $digits = array('', 'hundred','thousand','lakh', 'crore');
    while( $i < $digits_length ) {
        $divider = ($i == 2) ? 10 : 100;
        $number = floor($no % $divider);
        $no = floor($no / $divider);
        $i += $divider == 10 ? 1 : 2;
        if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
        } else $str[] = null;
    }
    $Rupees = implode('', array_reverse($str));
    $paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
    return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
}

I'm calling the function here:

                            <td valign="top" style="font-size:10px; width:50%; border-bottom:1px solid #000; border-right:1px solid #000;">
                                <table  width="100%" cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td valign="top" style="font-size:10px; width:30%; padding:5px; width:30%;"><b>Amount in words </b>:</td>
                                        <td valign="top" style="font-size:10px; width:30%; padding:5px; width:70%;"><?php if($invoice_info['net_amount'] !=0){echo "Rupees "; } echo getIndianCurrency($invoice_info['net_amount']); ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>

I'm getting this error:

A PHP Error was encountered
Severity: Notice

Message: A non well formed numeric value encountered

Filename: admin/invoice.php

Line Number: 15

Backtrace:

File: C:\xampp\htdocs\CK_Billing\application\views\admin\invoice.php
Line: 15
Function: _error_handler

File: C:\xampp\htdocs\CK_Billing\application\views\admin\invoice.php
Line: 271
Function: convertToIndianCurrency

File: C:\xampp\htdocs\CK_Billing\application\controllers\Admin.php
Line: 1428
Function: view

File: C:\xampp\htdocs\CK_Billing\index.php
Line: 315
Function: require_once

Thank you. Please help me to solve this error.

php

function

codeigniter

codeigniter-3

0 Answers

Your Answer

Accepted video resources