Is there an easy way to convert a number to a word

2019-01-02 21:14发布

In PHP, is there an easy way to convert a number to a word? For instance, 27 to twenty-seven.

标签: php numbers
13条回答
路过你的时光
2楼-- · 2019-01-02 21:21
Amount in Words:</b><?=no_to_words($number)?>

Very simple way to convert number to words using PHP function.

查看更多
弹指情弦暗扣
3楼-- · 2019-01-02 21:24

I rewrote the code above to fit the standard U.S. written word number format.

function singledigit($number){
    switch($number){
        case 0:$word = "zero";break;
        case 1:$word = "one";break;
        case 2:$word = "two";break;
        case 3:$word = "three";break;
        case 4:$word = "four";break;
        case 5:$word = "five";break;
        case 6:$word = "six";break;
        case 7:$word = "seven";break;
        case 8:$word = "eight";break;
        case 9:$word = "nine";break;
    }
    return $word;
}

function doubledigitnumber($number){
    if($number == 0){
        $word = "";
    }
    else{
        $word = "-".singledigit($number);
    }       
    return $word;
}

function doubledigit($number){
    switch($number[0]){
        case 0:$word = doubledigitnumber($number[1]);break;
        case 1:
            switch($number[1]){
                case 0:$word = "ten";break;
                case 1:$word = "eleven";break;
                case 2:$word = "twelve";break;
                case 3:$word = "thirteen";break;
                case 4:$word = "fourteen";break;
                case 5:$word = "fifteen";break;
                case 6:$word = "sixteen";break;
                case 7:$word = "seventeen";break;
                case 8:$word = "eighteen";break;
                case 9:$word = "ninteen";break;
            }break;
        case 2:$word = "twenty".doubledigitnumber($number[1]);break;                
        case 3:$word = "thirty".doubledigitnumber($number[1]);break;
        case 4:$word = "forty".doubledigitnumber($number[1]);break;
        case 5:$word = "fifty".doubledigitnumber($number[1]);break;
        case 6:$word = "sixty".doubledigitnumber($number[1]);break;
        case 7:$word = "seventy".doubledigitnumber($number[1]);break;
        case 8:$word = "eighty".doubledigitnumber($number[1]);break;
        case 9:$word = "ninety".doubledigitnumber($number[1]);break;

    }
    return $word;
}

function unitdigit($numberlen,$number){
    switch($numberlen){         
        case 3:case 6:case 9:case 12:$word = "hundred";break;
        case 4:case 5:$word = "thousand";break;
        case 7:case 8:$word = "million";break;
        case 10:case 11:$word = "billion";break;
    }
    return $word;
}

function numberToWord($number){

    $numberlength = strlen($number);
    if ($numberlength == 1) { 
        return singledigit($number);
    }elseif ($numberlength == 2) {
        return doubledigit($number);
    }
    else {

        $word = "";
        $wordin = "";
        switch ($numberlength ) {
        case 5:case 8:  case 11:
            if($number[0] >0){
                $unitdigit = unitdigit($numberlength,$number[0]);
                $word = doubledigit($number[0].$number[1]) ." ".$unitdigit." ";
                return $word." ".numberToWord(substr($number,2));
            }
            else{
                return $word." ".numberToWord(substr($number,1));
            }
        break;
        default:
            if($number[0] >0){
                $unitdigit = unitdigit($numberlength,$number[0]);
                $word = singledigit($number[0]) ." ".$unitdigit." ";
            }               
            return $word." ".numberToWord(substr($number,1));
        }
    }
}
查看更多
长期被迫恋爱
4楼-- · 2019-01-02 21:25

Using NumberFormatter class it is simple to get convert to words.

<?php

$number = '12345';
$locale = 'en_US';
$fmt = numfmt_create($locale, NumberFormatter::SPELLOUT);
$in_words = numfmt_format($fmt, $number);

print_r($in_words);
// twelve thousand three hundred forty-five

?>
查看更多
还给你的自由
5楼-- · 2019-01-02 21:28

Yes there is. without using a library you just need to follow this..

First you need to check in your server if ;extension=php_intl.dll is enabled in your php.ini if still not work you need to see this answer.

intl extension php_intl.dll with wamp

after successfully moving all the files starts with icu.

from: <wamp_installation_path>/bin/php/php5.4.3/

to: <wamp_installation_path>/bin/apache/apache2.2.22/bin/

and restart your server.

try to run this code:

$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format(123456);

it will show the output of one hundred twenty-three thousand four hundred fifty-six.

hope that helps everyone :).

查看更多
临风纵饮
6楼-- · 2019-01-02 21:29

You can use the NumberFormatter Class:

$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($myNumber);
查看更多
浅入江南
7楼-- · 2019-01-02 21:31

There is the Numbers_Words package in PECL. It does exactly what you ask for. The following languages are supported:

  • bg (Bulgarian) by Kouber Saparev
  • cs (Czech) by Petr 'PePa' Pavel
  • de (German) by Piotr Klaban
  • dk (Danish) by Jesper Veggerby
  • en_100 (Donald Knuth system, English) by Piotr Klaban
  • en_GB (British English) by Piotr Klaban
  • en_US (American English) by Piotr Klaban
  • es (Spanish Castellano) by Xavier Noguer
  • es_AR (Argentinian Spanish) by Martin Marrese
  • et (Estonian) by Erkki Saarniit
  • fr (French) by Kouber Saparev
  • fr_BE (French Belgium) by Kouber Saparev and Philippe Bajoit
  • he (Hebrew) by Hadar Porat
  • hu_HU (Hungarian) by Nils Homp
  • id (Indonesian) by Ernas M. Jamil and Arif Rifai Dwiyanto
  • it_IT (Italian) by Filippo Beltramini and Davide Caironi
  • lt (Lithuanian) by Laurynas Butkus
  • nl (Dutch) by WHAM van Dinter
  • pl (Polish) by Piotr Klaban
  • pt_BR (Brazilian Portuguese) by Marcelo Subtil Marcal and Mario H.C.T.
  • ru (Russian) by Andrey Demenev
  • sv (Swedish) by Robin Ericsson
查看更多
登录 后发表回答