开发者

Convert a number to its string representation

I'm developing a simple web application where in I need to display nu开发者_开发百科mber a to my users in string format.

Example:

12 - One Two or Twelve
-20 - minus Two zero or minus twenty

Either way is fine. I need this to be done in PHP. Any help will be appreciated.


for the first option (spell out digits), strtr is your friend

$words = array(
  '-' => 'minus ',
  '1'  => 'one ',
  '2' => 'two ',
etc....
);

echo strtr(-123, $words);


If you want to spell out the complete number you can make use of the PEAR Numbers_Words class. This class has a toWords() method that accepts a +ve or a -ve num and returns the spelled out string representation of the number.

If you want to convert the number to string digit wise, I am not aware of any lib function. But you can code one yourself easily. user187291 gives a good way to do this in his answer.

<?php

$arr = array(
        -12,
        20
            );

foreach($arr as $num) {
        $nw = new Numbers_Words();
    echo "$num = ". $nw->toWords($num)."\n";
}    

?>

Output:

C:\>php a.php
-12 = minus twelve
20 = twenty


bellow I am giving you an example function. It may not be a complete one but it should get you started (I know, the question has been posted long ago. still, it may help others - ) And I am sorry for any bugs :). and lastly, it is not finished one. I just post for an example starting point.

function convertToString($number, $blankIfZero=true){
        $strRep = "";
        $n = intval($number);       
        $one2twenty = array("One", "Two", "Three", "Four", 
                "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
                "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
                "Seventeen", "Eighteen", "Nineteen");
        $twenty2ninty = array("Twenty", "Thirty",
                "Fourty", "Fifty", "Sixty", "Seventy", "Eighty",
                "Ninety");
        $hundred = "Hundred";
        $thousand = "Thousand";
        $million = "Million";
        $billion = "Billion";

        switch($n){
            case 0: 
                if($blankIfZero == true){
                    $strRep= $strRep."";
                    break;
                }else{
                    $strRep = $strRep."Zero";
                    break;
                }
            case $n >0 && $n <20:
                $strRep = $strRep." ".$one2twenty[($n-1)];              
                break;
            case $n >=20 && $n < 100:               
                $strRep = $strRep . " ". $twenty2ninty[(($n/10) - 2)];
                $strRep .= convertToString($n%10);
                break;
            case $n >= 100 && $n <= 999:
                $strRep = $strRep.$one2twenty[(($n/100)-1)]." ".$hundred. " ";
                $strRep .= convertToString($n%100);
                break;
            case $n >= 1000 && $n < 100000:
                if($n < 20000){
                    $strRep = $strRep.$one2twenty[(($n/1000)-1)]." ".$thousand. " ";
                    $strRep .= convertToString($n%1000);
                    break;
                }else{
                    $strRep = $strRep . $twenty2ninty[(($n/10000) - 2)];
                    $strRep .= convertToString($n%10000);
                    break;
                }
            case $n >= 100000 && $n < 1000000:
                $strRep .= convertToString($n/1000). " ".$thousand. " ";
                $strRep .= convertToString(($n%100000)%1000);
                break;
            case $n >= 1000000 && $n <  10000000:                   
                $strRep = $strRep . $one2twenty[(($n/1000000) - 1)]. " ".$million." ";
                $strRep .= convertToString($n%1000000);
                    break;
            case $n >= 10000000 && $n < 10000000000:
                $strRep .= convertToString($n/1000000). " ".$million. " ";
                $strRep .= convertToString(($n%1000000));
                break;

        }

        return $strRep;
    }


Building on the work that @SRC did in a previous answer, I wrote a function to convert any raw unsigned integer into English, mainly out of curiosity.

You can find the code here: a GitHub Gist by StampyCode

This code is only limited by PHP's Integer limit (64-bit):

9223372036854775807

Which is processed by this code to output:

nine quintillion, two hundred and twenty three quadrillion, three hundred and seventy two trillion, thirty six billion, eight hundred and fifty four million, seven hundred and seventy five thousand, eight hundred and seven

Code embed:

<?php
$_1to19 = [
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen",
];
$_teen = [
    "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety",
];
$_mult = [
    2  => 'hundred',
    3  => 'thousand',
    6  => 'million',
    9  => 'billion',
    12 => 'trillion',
    15 => 'quadrillion',
    18 => 'quintillion',
    21 => 'sextillion',
    24 => 'septillion', // php can't count this high
    27 => 'octillion',
];
$fnBase = function ($n, $x) use (&$fn, $_mult) {
    return $fn($n / (10 ** $x)) . ' ' . $_mult[$x];
};
$fnOne = function ($n, $x) use (&$fn, &$fnBase) {
    $y = ($n % (10 ** $x)) % (10 ** $x);
    $s = $fn($y);
    $sep = ($x === 2 && $s ? " and " : ($y < 100 ? ($y ? " and " : '') : ', '));
    return $fnBase($n, $x) . $sep . $s;
};
$fnHundred = function ($n, $x) use (&$fn, &$fnBase) {
    $y = $n % (10 ** $x);
    $sep = ($y < 100 ? ($y ? ' and ' : '') : ', ');
    return ', ' . $fnBase($n, $x) . $sep . $fn($y);
};
$fn = function ($n) use (&$fn, $_1to19, $_teen, $number, &$fnOne, &$fnHundred) {
    switch ($n) {
        case 0:
            return ($number > 1 ? '' : 'zero');
        case $n < 20:
            return $_1to19[$n - 1];
        case $n < 100:
            return $_teen[($n / 10) - 2] . ' ' . $fn($n % 10);
        case $n < (10 ** 3):
            return $fnOne($n, 2);
    };
    for ($i = 4; $i < 27; ++$i) {
        if ($n < (10 ** $i)) {
            break;
        }
    }
    return ($i % 3) ? $fnHundred($n, $i - ($i % 3)) : $fnOne($n, $i - 3);
};
$number = $fn((int)$number);
$number = str_replace(', , ', ', ', $number);
$number = str_replace(',  ', ', ', $number);
$number = str_replace('  ', ' ', $number);
$number = ltrim($number, ', ');

return $number;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜