#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Location
    kuala lumpur
    Posts
    19
    Rep Power
    0

    Spell Number to Words


    Hi all,

    I want to spell this number:
    example USD 123456.20
    one hundred twenty three thousand four hundred fifty five dollar and twenty cent only

    anybody out there have write up the function? will appreciate if you can share here.
  2. #2
  3. Muhhnnn !!
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2003
    Posts
    1,530
    Rep Power
    89
    hehe, remind me one of the basic exercice we were used to have when learning c language in old times...

    I had so much fun with it that I will let you think about it a bit
    "The ultimate knowledge is reached when it does not bring new questions..."
    -- Usaphdas encyclopedia XV.4

  4. #3
  5. I love your chinese eyes :*
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2006
    Location
    Her heart... she claims!
    Posts
    1,668
    Rep Power
    1049
    I will let you think about it a bit
    that's very cruel of you .. he doesn't want to think on it therefore he came here to get it fast so if you have it then give it to him
  6. #4
  7. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    Somebody provided a function to do this a long while ago. Have you searched the forums?
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  8. #5
  9. Expert Debugger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2006
    Location
    Dev Shed Forums (-_^)v
    Posts
    1,022
    Rep Power
    1301
    Originally Posted by m4zw4n
    Hi all,

    I want to spell this number:
    example USD 123456.20
    one hundred twenty three thousand four hundred fifty five dollar and twenty cent only

    anybody out there have write up the function? will appreciate if you can share here.
    hi , there m4zw4n

    use this , you can get the logic:

    Code:
    $lakhs = floor($number / 100000);  /* lakhs (giga) */
          $number -= $lakhs * 100000;
          
          $thousands = floor($number / 1000);     /* Thousands (kilo) */
          $number -= $thousands * 1000;
          $hundreds = floor($number / 100);      /* Hundreds (hecto) */
          $number -= $hundreds * 100;
          $tens = floor($number / 10);       /* Tens (deca) */
          $ones = $number % 10;               /* Ones */
          $res = "";
          
          //echo "<hr>".$lakhs;
        
    
         if ($lakhs){
                
            $res .= $this->numToWords($lakhs) ;
            $res.=($lakhs>10)?" Lakhs":" Lakh";
          }
    
          if($thousands){
            $res .= (empty($res) ? "" : " ") .
            $this->numToWords($thousands) . " Thousand";
          }
    
          if ($hundreds){
            $res .= (empty($res) ? "" : " ") .
            $this->numToWords($hundreds) . " Hundred";
          }
    
          $arr_ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
          "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
          "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
          "Nineteen");
          $arr_tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
            "Seventy", "Eigthy", "Ninety");
    
        if ($tens || $ones){
            if (!empty($res)){
                $res .= " and ";
            }
    
            if ($tens < 2){
                $res .= $arr_ones[$tens * 10 + $ones];
            }
            else{
                $res .= $arr_tens[$tens];
                if ($ones){
                    $res .= "-" . $arr_ones[$ones];
                }
            }
        }
    
        if (empty($res)){
            $res = "zero";
        }
  10. #6
  11. Muhhnnn !!
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2003
    Posts
    1,530
    Rep Power
    89
    Blah that's not fun... there is even a version publish as comment in php manual =(

    Btw, shouldn't "Eightteen" be either "Eight teens" or "Eighteen" ?
    "The ultimate knowledge is reached when it does not bring new questions..."
    -- Usaphdas encyclopedia XV.4

  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    I just wrote something that handles the problem a little differently. It takes two functions in order to complete, but it handles the input as a string instead of a number, so all the processing is done on characters. This allows unreasonably large numbers to be processed easily.

    Now, the only limitation is that I haven't bothered to copy and paste all the designators and suffix designations because I'm lazy. All you have to do is continue to fill in the "million", "billion", etc. array and the "tenths", "hundredths", etc. array.

    Anyway, here's the code with some test cases at the bottom:
    PHP Code:
    //see the improved version below 
    -Dan
    Last edited by ManiacDan; January 5th, 2012 at 10:00 AM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    PHP Code:
    <?php

    //new and improved recursive version of my last attempt:
    function print_number_as_words2($number)
    {
        if ( !
    is_string($number) && !is_float($number) && !is_int($number) )
        {
            return 
    false;
        }

        if ( 
    is_string($number) )
        {
            
    //we know it's a string.  see if there's a negative:
            
    if ( substr($number01) == '-' )
            {
                
    $number substr($number1);
                
    $number $number * -1;
            }
        }
        
    $number strval($number);

        if ( 
    $number == '0' )
        {
            return 
    "Zero";
        }
        
    $negative $number "Negative" "";

        
    $number trim($number'-');

        
    $split_by_decimal explode("."$number);
        
    $decimal_string '';
        if ( 
    count($split_by_decimal) > )
        {
            
    $decimal_string process_decimal($split_by_decimal[1]);
        }
        return 
    trim(preg_replace("#\s+#"" "$negative " " process_number($split_by_decimal[0]) . " " $decimal_string));
    }

    function 
    process_number($number$depth 0)
    {
        
    $group_designators = array(
            
    "",
            
    "Thousand",
            
    "Million",
            
    "Billion",
            
    "Trillion",
            
    "Quadrillion",
            
    "Quintillion",
            
    "Sextillion",
            
    "Septillion",
            
    /*that's enough for now*/);

        
    $numbers = array(
            
    '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'=>"Fourty",
            
    '50'=>"Fifty",
            
    '60'=>"Sixty",
            
    '70'=>"Seventy",
            
    '80'=>"Eighty",
            
    '90'=>"Ninety",
            );

        
    //we already know that we have a numeric string.  Process it in groups of three characters
        
    while ( strlen($number) > )
        {
            if ( 
    strlen($number) <= )
            {
                
    $number_to_process $number;
                
    $number '';
            }
            else
            {
                
    $number_to_process substr($numberstrlen($number) - 3);
                
    $number substr($number0strlen($number) - 3);
            }

            if ( 
    strlen($number_to_process) == )
            {
                
    $output[] = $numbers[substr($number_to_process01)];
                
    $output[] = "Hundred";
                
    $number_to_process substr($number_to_process1);
            }

            if ( isset(
    $numbers[$number_to_process]) )
            {
                
    $output[] = $numbers[$number_to_process];
            }
            else
            {
                
    //we're dealing with a number greater than 20 and not divisible by 10:
                
    $tens substr($number_to_process01) . "0";
                
    $ones substr($number_to_process11);
                
    $output[] = $numbers[$tens];
                
    $output[] = $numbers[$ones];
            }
            return 
    process_number($number$depth+1) . " " implode(" "$output) . " " $group_designators[$depth];
        }
    }

    function 
    process_decimal($number)
    {
        
    $suffix = array(
            
    "Tenths",
            
    "Hundreths",
            
    "Thousandths",
            
    "Ten Thousandths",
            
    "Hundred Thousandths",
            
    "Millionths",
            
    //enough
            
    );
        return 
    " and " process_number($number) . $suffix[strlen($number) - 1];
    }

    echo 
    print_number_as_words2("-19832498347.34") . "\n";
    echo 
    print_number_as_words2(14.12) . "\n";
    echo 
    print_number_as_words2(1432489723485) . "\n";
    echo 
    print_number_as_words2(10234.45645) . "\n";
    echo 
    print_number_as_words2(-10.2) . "\n";
    echo 
    print_number_as_words2("1298721498732.111111") . "\n";
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo