Convert a number to its string representation

前端 未结 4 1714
野性不改
野性不改 2021-01-13 21:53

I\'m developing a simple web application where in I need to display number a to my users in string format.

Example:

12 - One Two or Twelve
-20 - min         


        
相关标签:
4条回答
  • 2021-01-13 22:22

    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
    
    0 讨论(0)
  • 2021-01-13 22:22

    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 讨论(0)
  • 2021-01-13 22:27

    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;
        }
    
    0 讨论(0)
  • 2021-01-13 22:39

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

    $words = array(
      '-' => 'minus ',
      '1'  => 'one ',
      '2' => 'two ',
    etc....
    );
    
    echo strtr(-123, $words);
    
    0 讨论(0)
提交回复
热议问题