dtd793353 2013-07-13 12:33
浏览 27
已采纳

在PHP中将数字转换为单词[关闭]

<?php
define("MAJOR", 'pounds');
define("MINOR", 'p');
class toWords
{
    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');

    function toWords($amount, $major = MAJOR, $minor = MINOR)
    {
        $this->major  = $major;
        $this->minor  = $minor;
        $this->number = number_format($amount, 2);
        list($this->pounds, $this->pence) = explode('.', $this->number);
        $this->words = " $this->major $this->pence$this->minor";
        if ($this->pounds == 0)
            $this->words = "Zero $this->words";
        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'))
                    $this->words = ' and ' . $this->words;
                $this->words = $this->_build($groups[$this->magind]) . $this->words;
            }
        }
    }

    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;
    }
}

Used:

$amount = 12345.67;
$obj    = new toWords($amount);
echo $obj->words; // gives Twelve thousand three hundred forty five pounds 67p
echo '<br/>';
echo $obj->number; // gives 12,345.67

Please the code above converts numbers to words but the problem I am faced with is how to change the last two digits after the dot (thus changing .67 to sixty seven) into words.Please I will appreciate if you could help me.Thanks

展开全部

  • 写回答

1条回答 默认 最新

  • dqkf49487 2013-07-13 13:47
    关注

    Naively, without dissecting your code too much, you could just call your existing code on the numbers to the left of the decimal, and then on the numbers to the right of the decimal, and then stick them together. It's a bit of a hack but it works:

    <?php
    define("MAJOR", 'pounds');
    define("MINOR", 'p');
    class toWords
    {
        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');
    
        function toWords($amount, $major = MAJOR, $minor = MINOR)
        {
            $this->__toWords__((int)($amount), $major);
            $whole_number_part = $this->words;
            #$right_of_decimal = (int)(($amount-(int)$amount) * 100);
            $strform = number_format($amount,2);
            $right_of_decimal = (int)substr($strform, strpos($strform,'.')+1);
            $this->__toWords__($right_of_decimal, $minor);
            $this->words = $whole_number_part . ' ' . $this->words;
        }
    
        function __toWords__($amount, $major)
        {
            $this->major  = $major;
            #$this->minor  = $minor;
            $this->number = number_format($amount, 2);
            list($this->pounds, $this->pence) = explode('.', $this->number);
            $this->words = " $this->major";
            if ($this->pounds == 0)
                $this->words = "Zero $this->words";
            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'))
                        $this->words = ' and ' . $this->words;
                    $this->words = $this->_build($groups[$this->magind]) . $this->words;
                }
            }
        }
    
        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;
        }
    }
    ?>
    

    Now your test works:

    $amount = 12345.67;
    $obj    = new toWords($amount);
    echo $obj->words; // gives twelve thousand three hundred and forty five  pounds  sixty seven  p
    echo '<br/>';
    echo $obj->number; // gives 12,345.67
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部