skip to Main Content

I got a big problem solving this algorithm in PHP the idea is when :

  1. a user input is < 1000 (999) the output should be nine hundred and ninety-nine millimes
  2. when the user input is > 999 the output should be **one dinnar **
    cause the dinnar is equal to 1000 millimes
    for example: if the input is 1,590 the output = one dinar and five hundred and ninety millimes
    another example if the input is 275,590 the output = two hundred and seventy-five dinars and five hundred and ninety millimes
    I write this code based on some article and I got a big error
<?php function numtowords($num){ 
$decones = array( 
            '01' => "One", 
            '02' => "Two", 
            '03' => "Three", 
            '04' => "Four", 
            '05' => "Five", 
            '06' => "Six", 
            '07' => "Seven", 
            '08' => "Eight", 
            '09' => "Nine", 
            10 => "Ten", 
            11 => "Eleven", 
            12 => "Twelve", 
            13 => "Thirteen", 
            14 => "Fourteen", 
            15 => "Fifteen", 
            16 => "Sixteen", 
            17 => "Seventeen", 
            18 => "Eighteen", 
            19 => "Nineteen" 
            );
$ones = 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" 
            ); 
$tens = array( 
            0 => "",
            2 => "Twenty", 
            3 => "Thirty", 
            4 => "Forty", 
            5 => "Fifty", 
            6 => "Sixty", 
            7 => "Seventy", 
            8 => "Eighty", 
            9 => "Ninety" 
            ); 
$hundreds = array( 
            "Hundred", 
            "Thousand", 
            "Million", 
            "Billion", 
            "Trillion", 
            "Quadrillion" 
            ); //limit t quadrillion 
$num = number_format($num,2,".",","); 
$num_arr = explode(".",$num); 
$wholenum = $num_arr[0]; 
$decnum = $num_arr[1]; 
$whole_arr = array_reverse(explode(",",$wholenum)); 
krsort($whole_arr); 
$rettxt = ""; 
foreach($whole_arr as $key => $i){ 
    if($i < 20){ 
        $rettxt .= $ones[$i]; 
    }
    elseif($i < 100){ 
        $rettxt .= $tens[substr($i,0,1)]; 
        $rettxt .= " ".$ones[substr($i,1,1)]; 
    }
    else{ 
        $rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0]; 
        $rettxt .= " ".$tens[substr($i,1,1)]; 
        $rettxt .= " ".$ones[substr($i,2,1)]; 
    } 
    if($key > 0){ 
        $rettxt .= " ".$hundreds[$key]." "; 
    } 

} 
$rettxt = $rettxt." dinar/s";

if($decnum > 0){ 
    $rettxt .= " and "; 
    if($decnum < 20){ 
        $rettxt .= $decones[$decnum]; 
    }
    elseif($decnum < 1000){ 
        $rettxt .= $tens[substr($decnum,0,1)]; 
        $rettxt .= " ".$ones[substr($decnum,1,1)]; 
    }
    $rettxt = $rettxt." FRANK/s"; 
} 
return $rettxt;} 

echo numtowords(275,590);



?>

the output here is Two Hundred Seventy Five dinar/s instead of two hundred and seventy-five dinars and five hundred and ninety millimes

2

Answers


  1. When you put a comma "," in a function call it’s considered as a parameters separator, so for the function numtowords(), you’re passing two parameters not one 275 & 590:

    echo numtowords(275,590);
    ___________________^
    

    So it takes just 275 into consideration and ignores the second parameter since the function declaration is made to receive just one parameter $num.

    The call must use the dot . instead :

    echo numtowords(275.590); 
    ___________________^
    
    Login or Signup to reply.
  2. You can use the NumberFormatter class with the NumberFormatter::SPELLOUT flag. As a base:

    $input = '275,590';
    $arr = explode(',',$input);
    
    $f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
    echo $f->format($arr[0]).' dinar and '.$f->format($arr[1]).' millimes ';
    //two hundred seventy-five dinar and five hundred ninety millimes
    

    Demo: https://3v4l.org/YR2WB

    I have not dealt with the case distinctions. I think that’s not the problem here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search