Link to home
Start Free TrialLog in
Avatar of sumitk
sumitk

asked on

Digit to words

Hi
My question is how do u convert any digit into word form.That means if we input 34526.78 the output should be thirty four thousand five hundred twenty six point seventy eight.

Sumit
Avatar of proskig
proskig

Try to see if the following would help

http://www.snippets.org/snippets/portable/portable.php3

Look under "Format ordinal numbers in English"
  It's not hard, if you are methodical about it.
   First of all, we'll need to define a general form of your input string.
   I assume it is something like this:

   NUMBER := [SIGN]DIGIT_SEQUENCE[.DIGIT_SEQUENCE1]

   SIGN := '+' | '-'

   DIGIT_SEQUENCE1 := '' | ONE_DIGIT[DIGIT_SEQUENCE1]

   DIGIT_SEQUENCE := ONE_DIGIT[DIGIT_SEQUENCE1]

   ONE_DIGIT := '0' | '1' | ... | '9'

   where the brackets mean optional and | means, of course OR.

   You will iterate the input, to check if it follows the above pattern and: remember whatever the number is signed, construct two for the integer part and decimal part, coresponding to DIGIT_SEQUENCE and DIGIT_SEQUENCE1, that, of course if you have a valid input.

   If you do, you have to strings representing natural numbers. Now you organize them in groups of three, I assume you know why, and the rest is common sense. You just need some constant character strings, and perform a series of tests. Shouldn't be hard.

   If it all works well, as a final touch, just be sure your output doesn't look like this:

   plus zero hundred three point one thousand

   when it should be:

   three point one

for the input +003.1000

   Good luck!


   
  Sorry for my spelling in the above, I didn't check before submit. I meant:

 "...construct two character strings for the integer part and decimal part..."

and

 "If you do, you have two strings representing..."

 Good luck, again.
Avatar of sumitk

ASKER

your answer is a bit complex and i am not able to make out anything out of it.I have started learning c recently.So please send me the source code in c so that i could understand the things better.
Avatar of DanRollins
if (x==1) printf("one");
if (x==2) printf("two");
...
if (x== 9999999) printf( "nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine" );

Just fill in the ... and bob's yer uncle!

-- Dan

P.S.  Note the pattern of groups of three.  You will need to work out how to code the strings for
        "zero"
to
        "nine hundred ninety-nine"
but the thousands and millions don't need separate logic (except to add "thousand" or "million" or "billion", etc.).
>> DanRollins. You forgot float numbers :)
>> sumitk. If DanRollins's solution will be too easy for you, then look at this function: itoa().
ASKER CERTIFIED SOLUTION
Avatar of zebada
zebada

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Quick change in the toWords() method:

      case 2:
        if ( words!="" )
          w = w+and;
        if ( *c=='1' )
          w = w+getTeens(*(c+1));
        if ( *c>'1' )
          w = w+getTens(*c);
        break;
One more in same method()

      case 2:
        if ( *c>='1' && words!="" )
          w = w+and;
if ((f/10)==1)  then printf("one tenth");
if ((f/10)==2)  then printf("two tenths");
if ( f=0.12345) then printf("zero point one two three four five");
if ( f==3.1415) then printf ("approximatately pi");
if ( f==666.666666) then printf ("Social security number of the beast");

Sorry, I can't help myself.  I will seek counseling.

-- Dan
And speling lesons :)
Hey!! Ppl, what are you doing? This is a homework problem, believe me.

Babu
HOMEWORK!  I never would have guessed.  And I put all of that effort into a comprehensive solution!  I thought this was

   About pie

-- Dan
Avatar of sumitk

ASKER

Thaks to give me complete source code.
Sumit
Doh! have I been duped - did I really do your homework sumtik? I even put comments in.

I guess this should have given it away "...I have started learning c recently..."

I didn't think it was homework, I had to write a routine to do exactly that for a cheque (check) printing program a few years ago - certainly not for homework.