How to convert decimal number to words (money form

2019-02-18 03:40发布

I just need a little help here. Because I am creating a code for converting decimals to Money format in words. For example if

I have this number

'2143.45'

the output should be

'two thousand one hundred forty three and forty-five cents'

I found a code like this but I don't have an idea how to include cents.

<?php

function convertNumber($number)
{
    list($integer, $fraction) = explode(".", (string) $number);

    $output = "";

    if ($integer{0} == "-")
    {
        $output = "negative ";
        $integer    = ltrim($integer, "-");
    }
    else if ($integer{0} == "+")
    {
        $output = "positive ";
        $integer    = ltrim($integer, "+");
    }

    if ($integer{0} == "0")
    {
        $output .= "zero";
    }
    else
    {
        $integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
        $group   = rtrim(chunk_split($integer, 3, " "), " ");
        $groups  = explode(" ", $group);

        $groups2 = array();
        foreach ($groups as $g)
        {
            $groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
        }

        for ($z = 0; $z < count($groups2); $z++)
        {
            if ($groups2[$z] != "")
            {
                $output .= $groups2[$z] . convertGroup(11 - $z) . (
                        $z < 11
                        && !array_search('', array_slice($groups2, $z + 1, -1))
                        && $groups2[11] != ''
                        && $groups[11]{0} == '0'
                            ? " and "
                            : ", "
                    );
            }
        }

        $output = rtrim($output, ", ");
    }

    if ($fraction > 0)
    {
        $output .= " point";
        for ($i = 0; $i < strlen($fraction); $i++)
        {
            $output .= " " . convertDigit($fraction{$i});
        }
    }

    return $output;
}

function convertGroup($index)
{
    switch ($index)
    {
        case 11:
            return " decillion";
        case 10:
            return " nonillion";
        case 9:
            return " octillion";
        case 8:
            return " septillion";
        case 7:
            return " sextillion";
        case 6:
            return " quintrillion";
        case 5:
            return " quadrillion";
        case 4:
            return " trillion";
        case 3:
            return " billion";
        case 2:
            return " million";
        case 1:
            return " thousand";
        case 0:
            return "";
    }
}

function convertThreeDigit($digit1, $digit2, $digit3)
{
    $buffer = "";

    if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
    {
        return "";
    }

    if ($digit1 != "0")
    {
        $buffer .= convertDigit($digit1) . " hundred";
        if ($digit2 != "0" || $digit3 != "0")
        {
            $buffer .= " and ";
        }
    }

    if ($digit2 != "0")
    {
        $buffer .= convertTwoDigit($digit2, $digit3);
    }
    else if ($digit3 != "0")
    {
        $buffer .= convertDigit($digit3);
    }

    return $buffer;
}

function convertTwoDigit($digit1, $digit2)
{
    if ($digit2 == "0")
    {
        switch ($digit1)
        {
            case "1":
                return "ten";
            case "2":
                return "twenty";
            case "3":
                return "thirty";
            case "4":
                return "forty";
            case "5":
                return "fifty";
            case "6":
                return "sixty";
            case "7":
                return "seventy";
            case "8":
                return "eighty";
            case "9":
                return "ninety";
        }
    } else if ($digit1 == "1")
    {
        switch ($digit2)
        {
            case "1":
                return "eleven";
            case "2":
                return "twelve";
            case "3":
                return "thirteen";
            case "4":
                return "fourteen";
            case "5":
                return "fifteen";
            case "6":
                return "sixteen";
            case "7":
                return "seventeen";
            case "8":
                return "eighteen";
            case "9":
                return "nineteen";
        }
    } else
    {
        $temp = convertDigit($digit2);
        switch ($digit1)
        {
            case "2":
                return "twenty-$temp";
            case "3":
                return "thirty-$temp";
            case "4":
                return "forty-$temp";
            case "5":
                return "fifty-$temp";
            case "6":
                return "sixty-$temp";
            case "7":
                return "seventy-$temp";
            case "8":
                return "eighty-$temp";
            case "9":
                return "ninety-$temp";
        }
    }
}

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

 $num = 500254.89;
 $test = convertNumber($num);

 echo $test;

?>

12条回答
smile是对你的礼貌
2楼-- · 2019-02-18 04:01
<?php

$price = mysqli_real_escape_string($conn, $_GET['price']);
$adprice = mysqli_real_escape_string($conn, $_GET['adprice']);
$number = $price-$adprice;

$no = round($number);
$point = round($number - $no, 2) * 100;
$hundred = null;
$digits_1 = strlen($no);
$i = 0;
$str = array();
$words = array('0' => '', '1' => 'One', '2' => 'Two',
    '3' => 'Three', '4' => 'Four', '5' => 'Five', '6' => 'Six',
    '7' => 'Seven', '8' => 'Eight', '9' => 'Nine',
    '10' => 'Ten', '11' => 'Eleven', '12' => 'Twelve',
    '13' => 'Thirteen', '14' => 'Fourteen',
    '15' => 'Fifteen', '16' => 'Sixteen', '17' => 'Seventeen',
    '18' => 'Eighteen', '19' =>'Nineteen', '20' => 'Twenty',
    '30' => 'Thirty', '40' => 'Forty', '50' => 'Fifty',
    '60' => 'Sixty', '70' => 'Seventy',
    '80' => 'Eighty', '90' => 'Ninety');
$digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
while ($i < $digits_1) {
$divider = ($i == 2) ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += ($divider == 10) ? 1 : 2;
if ($number) {
    $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
    $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
    $str [] = ($number < 21) ? $words[$number] .
                " " . $digits[$counter] . $plural . " " . $hundred
                :
                $words[floor($number / 10) * 10]
                . " " . $words[$number % 10] . " "
                . $digits[$counter] . $plural . " " . $hundred;
            } else $str[] = null;
              }
$str = array_reverse($str);
$result = implode('', $str);
$points = ($point) ?
          "." . $words[$point / 10] . " " . 
          $words[$point = $point % 10] : '';
          echo $result . "  " . $points . "Only ";
 ?> 
查看更多
Melony?
3楼-- · 2019-02-18 04:03

Follow the github link here, although it converts the whole number part only (which was intentional anyway), it can get you started, as it does not restrict the range of number and gives accurate result.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-18 04:06
// three hundred and thirteen rupees and seventy-six paise
echo getIndianCurrency(313.76);

function getIndianCurrency(float $number)
{
    $decimal = round($number - ($no = floor($number)), 2) * 100;
    $hundred = null;
    $digits_length = strlen($no);
    $i = 0;
    $str = array();
    $words = array(0 => '', 1 => 'one', 2 => 'two',
        3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
        7 => 'seven', 8 => 'eight', 9 => 'nine',
        10 => 'ten', 11 => 'eleven', 12 => 'twelve',
        13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
        16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
        19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
        40 => 'forty', 50 => 'fifty', 60 => 'sixty',
        70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
    $digits = array('', 'hundred','thousand','lakh', 'crore');

    while( $i < $digits_length ) {
        $divider = ($i == 2) ? 10 : 100;
        $number = floor($no % $divider);
        $no = floor($no / $divider);
        $i += $divider == 10 ? 1 : 2;
        if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
        } else $str[] = null;
    }

    $rupees = implode('', array_reverse($str));
    $paise = '';

    if ($decimal) {
        $paise = 'and ';
        $decimal_length = strlen($decimal);

        if ($decimal_length == 2) {
            if ($decimal >= 20) {
                $dc = $decimal % 10;
                $td = $decimal - $dc;
                $ps = ($dc == 0) ? '' : '-' . $words[$dc];

                $paise .= $words[$td] . $ps;
            } else {
                $paise .= $words[$decimal];
            }
        } else {
            $paise .= $words[$decimal % 10];
        }

        $paise .= ' paise';
    }

    return ($rupees ? $rupees . 'rupees ' : '') . $paise ;
}

Uses:

echo getIndianCurrency(313.76);

Output:

three hundred and thirteen rupees and seventy-six paise
查看更多
姐就是有狂的资本
5楼-- · 2019-02-18 04:07
For Indian Currnecy 
For Indian Currency
<html>
<head>
<title> Number to word converter</title>
</head>
<body>
<?php
$ones =array('',' One',' Two',' Three',' Four',' Five',' Six',' Seven',' Eight',' Nine',' Ten',' Eleven',' Twelve',' Thirteen',' Fourteen',' Fifteen',' Sixteen',' Seventeen',' Eighteen',' Nineteen');
$tens = array('','',' Twenty',' Thirty',' Fourty',' Fifty',' Sixty',' Seventy',' Eighty',' Ninety',);
$triplets = array('',' Thousand',' Lac',' Crore',' Arab',' Kharab');


function Show_Amount_In_Words($num) {
  global $ones, $tens, $triplets;
$str ="";


//$num =(int)$num;
$th= (int)($num/1000); 
$x = (int)($num/100) %10;
$fo= explode('.',$num);

if($fo[0] !=null){
$y=(int) substr($fo[0],-2);

}else{
    $y=0;
}

if($x > 0){
    $str =$ones[$x].' Hundred';

}
if($y>0){
if($y<20)
{
 $str .=$ones[$y];

}
else {
    $str .=$tens[($y/10)].$ones[($y%10)];
   }
}
$tri=1;
while($th!=0){

    $lk = $th%100;
    $th = (int)($th/100);
    $count =$tri;

    if($lk<20){
        if($lk == 0){
        $tri =0;}
        $str = $ones[$lk].$triplets[$tri].$str;
        $tri=$count;
        $tri++;
    }else{
        $str = $tens[$lk/10].$ones[$lk%10].$triplets[$tri].$str;
        $tri++;
    }
}
$num =(float)$num;
if(is_float($num)){
     $fo= (String) $num;
      $fo= explode('.',$fo);
       $fo1= @$fo[1];

}else{
    $fo1 =0;
}
$check = (int) $num;
 if($check !=0){
          return $str.' Rupees'.forDecimal($fo1);
    }else{
       return forDecimal($fo1);
    }
}//End function Show_Amount_In_Words

if(isset($_POST['num'])){
   $num = $_POST['num'];
 echo Show_Amount_In_Words($num);
 }



//function for decimal parts
 function forDecimal($num){
    global $ones,$tens;
    $str="";
    $len = strlen($num);
    if($len==1){
        $num=$num*10;
    }
    $x= $num%100;
    if($x>0){
    if($x<20){
        $str = $ones[$x].' Paise';
    }else{
        $str = $tens[$x/10].$ones[$x%10].' Paise';
    }
    }
     return $str;
 }  
?>
<form action='#' method='post'>
<input type='text' name='num' size='20'>
<input type='submit' name='submit' />
</form>
</body>
</html>
查看更多
一夜七次
6楼-- · 2019-02-18 04:09

I tried to add something for the decimal value. Hope it will help.

<?php
function convertNumber($number)
{
    list($integer,$fraction) = explode(".", (string) $number);

    $output = " ";

    if ($integer{0} == "-")
    {
        $output = "negative ";
        $integer    = ltrim($integer, "-");
    }
    else if ($integer{0} == "+")
    {
        $output = "positive ";
        $integer    = ltrim($integer, "+");
    }

    if ($integer{0} == "0")
    {
        $output .= "zero";
    }
    else
    {
        $integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
        $group   = rtrim(chunk_split($integer, 3, " "), " ");
        $groups  = explode(" ", $group);

        $groups2 = array();
        foreach ($groups as $g)
        {
            $groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
        }

        for ($z = 0; $z < count($groups2); $z++)
        {
            if ($groups2[$z] != "")
            {
                $output .= $groups2[$z] . convertGroup(11 - $z) . (
                        $z < 11
                        && !array_search('', array_slice($groups2, $z + 1, -1))
                        && $groups2[11] != ''
                        && $groups[11]{0} == '0'
                            ? " and "
                            : " "
                    );
            }
        }

        $output = rtrim($output, ", ");
    }

    if ($fraction > 0)
    {
        $output .= " PESOS ";
        for ($i = 0; $i < strlen($fraction); $i++)
        {
           if($fraction==01){

               $i = 1;
                while ($i <2):
                     $output .= " and one centavo only";

                 $i++;
                endwhile;
           }
           else if($fraction==02){

               $i = 1;
                while ($i <2):
                     $output .= " and two centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==03){

               $i = 1;
                while ($i <2):
                     $output .= " and three centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==04){

               $i = 1;
                while ($i <2):
                         $output .= " and four centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==05){

               $i = 1;
                while ($i <2):
                         $output .= " and five centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==06){

               $i = 1;
                while ($i <2):
                         $output .= " and six centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==07){

              $i = 1;
                while ($i <2):
                         $output .= " point seven centavos only";

                 $i++;
                endwhile;
           }
            else if($fraction==8 || $fraction==08){

               $i = 1;
                while ($i <2):
                 $output .= " and eight centavos only";

                 $i++;
                endwhile;
           }
            else if($fraction==9 || $fraction==09){

               $i = 1;
                while ($i <2):
                 $output .= " and nine centavos only";

                 $i++;
                endwhile;
           }
            else if($fraction==10){

                  $i = 1;
                while ($i <2):
                  $output .= " and ten centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==11){

              $i = 1;
                while ($i <2):
                         $output .= " and eleven centavos only";

                 $i++;
                endwhile;
           }
             else if($fraction==12){

                 $i = 1;
                while ($i <2):
                     $output .= " and twelve centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==13){

             $i = 1;
                while ($i <2):
                         $output .= " and thirteen centavos only";

                 $i++;
                endwhile;
           }
            else if($fraction==14){

               $i = 1;
                while ($i <2):
                         $output .= " and fourteen centavos only";
                 $i++;
                endwhile;
           }
           else if($fraction==15){

               $i = 1;
                while ($i <2):
                         $output .= " and fifteen centavos only";

                 $i++;
                endwhile;
           }
           else if($fraction==16){

               $i = 1;
                while ($i <2):
                         $output .= " and sixteen centavos only";
                 $i++;
                endwhile;
           }
           else if($fraction==17){


               $i = 1;
                while ($i <2):
                     $output .= " and seventeen centavos only";
                 $i++;
                endwhile;
           }
           else if($fraction==18){


               $i = 1;
                while ($i <2):
                $output .= " and eighteen centavos only";
                 $i++;
                endwhile;
           }
           else if($fraction==19){

               $i = 1;
                while ($i <2):
                         $output .= " and nineteen centavos only";
                 $i++;
                endwhile;
           }
           else if($fraction==20){

               $i = 1;
                while ($i <2):
                     $output .= " and twenty centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==21){
               $i = 1;
                while ($i <2):
                  $output .= " and twenty one centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==22){

              $i = 1;
                while ($i <2):
                         $output .= " and twenty two centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==23){
                 $i = 1;
                while ($i <2):
                         $output .= " and twenty three centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==24){

                 $i = 1;
                while ($i <2):
                     $output .= " and twenty four centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==25){

             $i = 1;
                while ($i <2):
                 $output .= " and twenty five centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==26){

                $i = 1;
                while ($i <2):
                    $output .= " and twenty six centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==27){

                $i = 1;
                while ($i <2):
                 $output .= " and twenty seven centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==28){

                $i = 1;
                while ($i <2):
                    $output .= " and twenty eight centavos only";
                 $i++;
                endwhile;
           }
            else if($fraction==29){

                $i = 1;
                while ($i <2):
                     $output .= " and twenty nine centavos only";
                    $i++;
                endwhile;
           }
             else if($fraction ==30){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==31){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty one centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==32){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty two centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==33){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty three centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==34){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty four centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==35){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty five centavos only";
                    $i++;
                endwhile;
           }
             else if($fraction ==36){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty six centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==37){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty seven centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==38){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty eight centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==39){

                $i = 1;
                while ($i <2):
                     $output .= " and thirty nine centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==40){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==41){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty one centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==42){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty two centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==43){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty three centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==44){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty four centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==45){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty five centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==46){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty six centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==47){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty seven centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==48){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty eight centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==49){

                $i = 1;
                while ($i <2):
                     $output .= " and fourty nine centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==50){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==51){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty one centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==52){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty two centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==53){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty three centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==54){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty four centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==55){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty five centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==56){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty six centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==57){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty seven centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==58){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty eight centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==59){

                $i = 1;
                while ($i <2):
                     $output .= " and fifty nine centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==60){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty centavos only";
                    $i++;
                endwhile;
           }
          else if($fraction ==61){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty one centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==62){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty two centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==63){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty three centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==64){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty four centavos only";
                    $i++;
                endwhile;
           }
          else if($fraction ==65){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty five centavos only";
                    $i++;
                endwhile;
           }
         else if($fraction ==66){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty six centavos only";
                    $i++;
                endwhile;
           }

         else if($fraction ==67){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty seven centavos only";
                    $i++;
                endwhile;
           }
        else if($fraction ==68){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty eight centavos only";
                    $i++;
                endwhile;
           }
        else if($fraction ==69){

                $i = 1;
                while ($i <2):
                     $output .= " and sixty nine centavos only";
                    $i++;
                endwhile;
           }
       else if($fraction ==70){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy centavos only";
                    $i++;
                endwhile;
           }
     else if($fraction ==71){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy one centavos only";
                    $i++;
                endwhile;
           }

     else if($fraction ==72){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy two centavos only";
                    $i++;
                endwhile;
           }
    else if($fraction ==73){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy three centavos only";
                    $i++;
                endwhile;
           }
     else if($fraction ==74){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy four centavos only";
                    $i++;
                endwhile;
           }

            else if($fraction ==75){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy five centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==76){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy six centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==77){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy seven centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==78){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy eight centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==79){

                $i = 1;
                while ($i <2):
                     $output .= " and seventy nine centavos only";
                    $i++;
                endwhile;
           }
            else if($fraction ==80){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty centavos only";
                    $i++;
                endwhile;
           }
         else if($fraction ==81){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty one centavos only";
                    $i++;
                endwhile;
           }
        else if($fraction ==82){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty two centavos only";
                    $i++;
                endwhile;
           }
       else if($fraction ==83){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty three centavos only";
                    $i++;
                endwhile;
           }
      else if($fraction ==84){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty four centavos only";
                    $i++;
                endwhile;
           }
        else if($fraction ==85){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty five centavos only";
                    $i++;
                endwhile;
           }
         else if($fraction ==86){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty six centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==87){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty seven centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==88){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty eight centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==89){

                $i = 1;
                while ($i <2):
                     $output .= " and eighty nine centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==90){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==91){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety one centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==92){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety two centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==93){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety three centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==94){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety four centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==95){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety five centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==96){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety six centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==97){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety seven centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==98){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety eight centavos only";
                    $i++;
                endwhile;
           }
           else if($fraction ==99){

                $i = 1;
                while ($i <2):
                     $output .= " and ninety nine centavos only";
                    $i++;
                endwhile;
           }

        }

    }
      else{
            $output .= " PESOS ONLY";
           }

    return $output;
}

function convertGroup($index)
{
    switch ($index)
    {
        case 11:
            return " decillion";
        case 10:
            return " nonillion";
        case 9:
            return " octillion";
        case 8:
            return " septillion";
        case 7:
            return " sextillion";
        case 6:
            return " quintrillion";
        case 5:
            return " quadrillion";
        case 4:
            return " trillion";
        case 3:
            return " billion";
        case 2:
            return " million";
        case 1:
            return " thousand";
        case 0:
            return "";
    }
}

function convertThreeDigit($digit1, $digit2, $digit3)
{
    $buffer = " ";

    if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
    {
        return "";
    }

    if ($digit1 != "0")
    {
        $buffer .= convertDigit($digit1) . " hundred";
        if ($digit2 != "0" || $digit3 != "0")
        {
            $buffer .= " ";
        }
    }

    if ($digit2 != "0")
    {
        $buffer .= convertTwoDigit($digit2, $digit3);
    }
    else if ($digit3 != "0")
    {
        $buffer .= convertDigit($digit3);
    }

    return $buffer;
}

function convertTwoDigit($digit1, $digit2)
{
    if ($digit2 == "0")
    {
        switch ($digit1)
        {
            case "1":
                return "ten";
            case "2":
                return "twenty";
            case "3":
                return "thirty";
            case "4":
                return "forty";
            case "5":
                return "fifty";
            case "6":
                return "sixty";
            case "7":
                return "seventy";
            case "8":
                return "eighty";
            case "9":
                return "ninety";
        }
    } else if ($digit1 == "1")
    {
        switch ($digit2)
        {
            case "1":
                return "eleven";
            case "2":
                return "twelve";
            case "3":
                return "thirteen";
            case "4":
                return "fourteen";
            case "5":
                return "fifteen";
            case "6":
                return "sixteen";
            case "7":
                return "seventeen";
            case "8":
                return "eighteen";
            case "9":
                return "nineteen";
        }
    } else
    {
        $temp = convertDigit($digit2);
        switch ($digit1)
        {
            case "2":
                return "twenty $temp";
            case "3":
                return "thirty $temp";
            case "4":
                return "forty $temp";
            case "5":
                return "fifty $temp";
            case "6":
                return "sixty $temp";
            case "7":
                return "seventy $temp";
            case "8":
                return "eighty $temp";
            case "9":
                return "ninety $temp";
        }
    }
}

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

}
strtoupper(convertNumber(2143.45)

?>
查看更多
一纸荒年 Trace。
7楼-- · 2019-02-18 04:10

If you don't want to edit the function, a quick and dirty way you can do this is by hacking the parts where it prints the words, like this: http://pastebin.com/UwSR8NpV It should work like the way you wanted it to.

查看更多
登录 后发表回答