将印度货币转换为php中的单词

时间:2016-06-14 11:52:11

标签: php arrays currency

我试图将货币换成单词,我可以使用下面的代码成功转换,但是, 货币主要显示为十万,1缺乏(100,000),如何更改它显示1缺乏(1,00,000)而不是十万。

    <?php defined('BASEPATH') OR exit('No direct script access allowed');

class Wllword
{
     var $pounds;
     var $pence;
     var $major;
     var $minor;
     var $words = '';
     var $number;
     var $magind;
     var $units = array('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine');
     var $teens = array('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
     var $tens = array('', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety');
     var $mag = array('', 'Thousand', 'Million', 'Billion', 'Trillion');

    public function __construct()
    {
        $this->CI =& get_instance();
    }
    public function makeWord($amount, $major = "", $minor = "")
    {
        $this->__toWords__((int)($amount), $major);
        $whole_number_part = $major.$this->words;

        $strform = number_format($amount,2);
        $right_of_decimal = (int)substr($strform, strpos($strform,'.')+1);

        $this->__toWords__($right_of_decimal, $minor);

       return $whole_number_part . ' Only';
       //return $whole_number_part . ' And ' .$minor. $this->words.' Only'; //Edited By Sha
    }

   private function __toWords__($amount, $major = '')
    {
        $this->major  = $major;
        $this->number = number_format($amount, 2);
        list($this->pounds, $this->pence) = explode('.', $this->number);
        $this->words = " ";
        //$this->words = "$this->major ";
        if ($this->pounds == 0)
            $this->words = "$this->words Zero";
        else {
            $groups = explode(',', $this->pounds);
            $groups = array_reverse($groups);
            for ($this->magind = 0; $this->magind < count($groups); $this->magind++) {
                if (($this->magind == 1) && (strpos($this->words, 'Hundred') === false) && ($groups[0] != '000'))
                  //print $this->words;
                   $this->words = ' And ' . $this->words;
                $this->words = $this->_build($groups[$this->magind]) . $this->words;
            }
        }
    }

  private function _build($n)
    {
        $res = '';
        $na  = str_pad("$n", 3, "0", STR_PAD_LEFT);
        if ($na == '000')
            return '';
        if ($na{0} != 0)
            $res = ' ' . $this->units[$na{0}] . ' Hundred';
        if (($na{1} == '0') && ($na{2} == '0'))
            return $res . ' ' . $this->mag[$this->magind];
        $res .= $res == '' ? '' : ' And';
        $t = (int) $na{1};
        $u = (int) $na{2};
        switch ($t) {
            case 0:
                $res .= ' ' . $this->units[$u];
                break;
            case 1:
                $res .= ' ' . $this->teens[$u];
                break;
            default:
                $res .= ' ' . $this->tens[$t] . ' ' . $this->units[$u];
                break;
        }
        $res .= ' ' . $this->mag[$this->magind];
        return $res;
    }
}
?>

任何帮助都可能非常感谢。

0 个答案:

没有答案