Convert a number to its string representation

前端 未结 4 1738
野性不改
野性不改 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条回答
  •  旧时难觅i
    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;
        }
    

提交回复
热议问题