Google Answers Logo
View Question
 
Q: Computer Algorithm ( Answered,   0 Comments )
Question  
Subject: Computer Algorithm
Category: Computers > Algorithms
Asked by: fdelucio-ga
List Price: $10.00
Posted: 11 Sep 2002 15:54 PDT
Expires: 11 Oct 2002 15:54 PDT
Question ID: 64057
What is the algorithm to translate (convert) numbers into letters. For
example: 1,000 = one thousand
256 = two hundred fifty six
10,101 ten thousand one hundred one

Request for Question Clarification by pinkfreud-ga on 11 Sep 2002 16:12 PDT
Regarding an algorithm to convert numbers into alphabetic words, what
computer language do you prefer?

Clarification of Question by fdelucio-ga on 11 Sep 2002 16:56 PDT
pseudo-code

Request for Question Clarification by pinkfreud-ga on 11 Sep 2002 17:30 PDT
I have found links for algorithms that convert numbers into their
verbal equivalents in Visual Basic, Excel, MS Access, COBOL, AS/400
RPG, Javascript, and C++.

Would any of those be of use to you?
Answer  
Subject: Re: Computer Algorithm
Answered By: eiffel-ga on 12 Sep 2002 03:53 PDT
 
Hi fdelucio,

There are various different styles of pseudocode. If you have a
specific style in mind, please post a sample of it (as a clarification
request) and I'll adapt this algorithm to your style.

This algorithm converts integers from 1 to 999,999 into words. Phrases
starting with "//" are comments. The "DIV" operator represents integer
division with the remainder discarded. The "+" operator represents
string concatenation.

The general approach is to break down the problem into a number of
simpler conversion tasks, then use recursive calls to this same
function to perform the subtasks, then concatenate (join) the
fragments into the final answer.

There are a lot of "special cases" to watch out for, to make sure that
(for example) 5000 doesn't get formatted as "five thousand zero".

integer2words(i)
// Returns a string representing the integer 'i' spelled as words.
// Local variables used: thousands, hundreds, tens, units, result
if i < 1 or i > 999999 then
   report_error_and_exit("out of range")
end if
thousands := i DIV 1000
units := i - (thousands * 1000)
if thousands > 0 then
   result := integer2words(thousands) + " thousand"
   if units > 0 then
      result := result + " " + integer2words(units)
   end if
else   // The range is from 1 to 999.
   hundreds := units DIV 100
   units := units - (hundreds * 100)
   if hundreds > 0 then
      result := integer2words(hundreds) + " hundred"
      if units > 0 then
         result := result + " " + integer2words(units)
      end if
   else   // The range is from 1 to 99.
      if units = 1 then result := "one"
      else if units = 2 then result := "two"
      else if units = 3 then result := "three"
      else if units = 4 then result := "four"
      else if units = 5 then result := "five"
      else if units = 6 then result := "six"
      else if units = 7 then result := "seven"
      else if units = 8 then result := "eight"
      else if units = 9 then result := "nine"
      else if units = 10 then result := "ten"
      else if units = 11 then result := "eleven"
      else if units = 12 then result := "twelve"
      else if units = 13 then result := "thirteen"
      else if units = 14 then result := "fourteen"
      else if units = 15 then result := "fifteen"
      else if units = 16 then result := "sixteen"
      else if units = 17 then result := "seventeen"
      else if units = 18 then result := "eighteen"
      else if units = 19 then result := "nineteen"
      else   // The range is from 20 to 99
         tens := units DIV 10
         units := units - (tens * 10)
         if tens = 2 then result := "twenty"
         elseif tens = 3 then result := "thirty"
         elseif tens = 4 then result := "forty"
         elseif tens = 5 then result := "fifty"
         elseif tens = 6 then result := "sixty"
         elseif tens = 7 then result := "seventy"
         elseif tens = 8 then result := "eighty"
         elseif tens = 9 then result := "ninety"
         end if
         if units > 0 then
            result := result + " " + integer2words(units)
         end if
      end if
   end if
end if
return(result)


Additional links:

"Numbers to words" in PL/SQL
http://anlab.hufs.ac.kr/~yjkim/BOOK/books/oracle/prog2/ch17_08.htm

"Numbers to words" in RPG
http://www.as400pro.com/nbrtowords.htm

"Numbers to words" in C
http://akbani.20m.com/codes/numword.txt

"Numbers to words" in Delphi
http://www.howtodothings.com/showarticle.asp?article=306

"Numbers to words" in Visual Basic
http://www.vbce.com/code/functions/convert_numbers_to_words/index.asp

"Numbers to words" in Java
http://mindprod.com/inwords.html

"Numbers to words" in C++
http://rossm.net/Electronics/Computers/Software/C++/Algorithms/index.htm#itow


Google search strategy:

"numbers to words" algorithm
://www.google.com/search?q=%22numbers%20to%20words%22%20algorithm
(unsuccessful)

"numbers to english" algorithm
://www.google.com/search?q=%22numbers+to+english%22+algorithm
(unsuccessful)

"numbers to words"
://www.google.com/search?client=googlet&q=%22numbers%20to%20words%22


Regards,
eiffel-ga
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy