Help pls..Numbers into words

Started by Believil, September 05, 2011, 11:47:28 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Believil

Hello
  I would like to know How do I change Numbers into words,  I have a programming code that does this but I can't get it to work so I want to change it in to a java code instead so it can work on internet explorer page.

Any help would be great thanks
Carl

My computer as :- Windows XP service pack 3
                             AMD Athlon xp 2600+
                             2.11 GHZ - 1.50 GB of Ram
                             Nvidia Geforce 4 Ti 4200

Allan

Please don't post the same question more than once. I deleted the other topic.

kpac


Believil

Sorry about that Allan

Hi Kpac I don't mind which ever one is easier to do.
My computer as :- Windows XP service pack 3
                             AMD Athlon xp 2600+
                             2.11 GHZ - 1.50 GB of Ram
                             Nvidia Geforce 4 Ti 4200

kpac

No, I meant which one were you talking about?
QuoteI want to change it in to a java code instead so it can work on internet explorer page.

Also, post the code you have at the moment.

Believil

Ok Well I'm new to coding so I guess javascript

My Programming Code Is......

/*
*  Function:    number_to_words
*  Description:  recursive numerics to words function
*  Converts a given integer (in range [0..1T-1], inclusive) into
*  alphabetical format ("one", "two", etc.)
*  @int
*  @return string
*/

function number_to_words($number)
{
    if (($number < 0) || ($number > 999999999))
    {
       throw new Exception("Number is out of range");
    }

    $Gn = floor($number / 1000000);  /* Millions (giga) */
    $number -= $Gn * 1000000;
    $kn = floor($number / 1000);     /* Thousands (kilo) */
    $number -= $kn * 1000;
    $Hn = floor($number / 100);      /* Hundreds (hecto) */
    $number -= $Hn * 100;
    $Dn = floor($number / 10);       /* Tens (deca) */
    $n = $number % 10;               /* Ones */

    $result = "";

    if ($Gn)
    {  $result .= number_to_words($Gn) . " Million";  }

    if ($kn)
    {  $result .= (empty($result) ? "" : " ") . number_to_words($kn) . " Thousand"; }

    if ($Hn)
    {  $result .= (empty($result) ? "" : " ") . number_to_words($Hn) . " Hundred";  }

    $ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
        "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
        "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
        "Nineteen");
    $tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
        "Seventy", "Eigthy", "Ninety");

    if ($Dn || $n)
    {
       if (!empty($result))
       {  $result .= " and ";
       }

       if ($Dn < 2)
       {  $result .= $ones[$Dn * 10 + $n];
       }
       else
       {  $result .= $tens[$Dn];
          if ($n)
          {  $result .= "-" . $ones[$n];
          }
       }
    }

    if (empty($result))
    {  $result = "zero"; }

    return $result;
}
My computer as :- Windows XP service pack 3
                             AMD Athlon xp 2600+
                             2.11 GHZ - 1.50 GB of Ram
                             Nvidia Geforce 4 Ti 4200

kpac

That's PHP. All you do is called the number_to_words function, i.e.

<?php
  number_to_words
(560); 
?>

...should return "five hundred and sixty".

Believil

yea but What I mean Is,
  I've opened up text Document and typed in the html,head,script,body & html the Basic stuff and I've placed it in between the <script>Placed it here</script> and when I open It it doesn't do anything just shows me the code.

I don't know anything about php.
My computer as :- Windows XP service pack 3
                             AMD Athlon xp 2600+
                             2.11 GHZ - 1.50 GB of Ram
                             Nvidia Geforce 4 Ti 4200

kpac

QuoteI've opened up text Document and typed in the html,head,script,body & html the Basic stuff and I've placed it in between the <script>Placed it here</script> and when I open It it doesn't do anything just shows me the code.
That's the problem. :)

Here: http://javascript.about.com/library/bltoword.htm
More: Goooooogle

Believil

yea I was hoping to change 1-24 into words that I type in ie:- 1 = pasta 2 = chips 3 = pie 4 = soup 5 = shreddies etc etc

Say I type 24 words and it changes all my numbers into those words
My computer as :- Windows XP service pack 3
                             AMD Athlon xp 2600+
                             2.11 GHZ - 1.50 GB of Ram
                             Nvidia Geforce 4 Ti 4200

Rob Pomeroy

To be honest, all the hard work has been done for you.  Converting the code from PHP to javascript is not that difficult.

Do you want someone to do the whole exercise for you?  You might be interested in trying http://www.vworker.com/ - there are coders in India that would do something like this for $5 or so.
Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos