We help IT Professionals succeed at work.

Digit to words

sumitk
sumitk asked
on
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
Comment
Watch Question

Commented:
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.

Author

Commented:
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.
CERTIFIED EXPERT
Author of the Year 2009

Commented:
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.).

Commented:
>> DanRollins. You forgot float numbers :)

Commented:
>> sumitk. If DanRollins's solution will be too easy for you, then look at this function: itoa().
Commented:
This code is not 100% tested but it should get you close to what you want.

#include <string>
#include <iostream>
#include <strstream>

using namespace std;

class numWords {
private:
  string num;

  string toWords(string tuple,int tupleCount);
  string getUnits(char index);
  string getTeens(char index);
  string getTens(char index);
  string getFactors(int index);

public:
  numWords() {numWords("");};
  numWords(string aNum) {num=aNum;};
  ~numWords(void) {};

  void setNum(double aNum) {strstream i; i<<aNum; num=i.str();};
  void setNum(string aNum) {num=aNum;};
  string getNum(void) {return num;};

  string asText(double aNum) {setNum(aNum); return asText();};
  string asText(string aNum) {setNum(aNum); return asText();};
  string asText(void);
};

#define MAX_FACTORS 5
string units[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
string teens[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string tens[] = {"<units>","<teens>","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
string hundred = "hundred";
string and = "and ";
string point = "point";
string factors[] = {"","thousand","million","billion","trillion"};

// _________________________________________________________________________
int
main(int argc, char *argv[])
{
  class numWords nw;
  int            i;
  char           f[32];

  for ( i=1; i<argc; i++ )
    cout << nw.asText(argv[i]) << endl;

  return 0;
}

// _________________________________________________________________________
string
numWords::asText()
{
  // This function assumes the string is "normalised".
  // i.e. no leading zeros no negative sign, no trailing zeros after the
  //      decimal point.

  string::iterator c;
  int              p,i;
  string           tuple="";
  int              tupleCount=0;
  string           w,words="";

  // Starting with the "units" digit immediately to the left of the decimal point....
  if ( (p=num.find_first_of(".",0))<0 )
    c = num.end()-1;
  else
    c = num.begin()+p-1;

  while ( num.begin()<=c ) {
    // Extract tuples (groups of three digits)
    tuple = "";
    for (i=0 ; i<3 && num.begin()<=c ; i++,c-- )
      tuple = *c+tuple;

    // Depending on the tupleCount this tuple represents thousands, millions, billions etc
    w = toWords(tuple,tupleCount++);
    if ( w!="" )
      words = w+" "+words;
  }

  // Starting with the "fractional" digit immediately to the right of the decimal point....
  if ( (c=num.begin()+p+1)<num.end() )
    words = words+point;
  for ( c=num.begin()+p+1 ; c<num.end() ; c++ )
    words = words+" "+getUnits(*c);
  return words;
}

// _________________________________________________________________________
string
numWords::toWords(string tuple, int tupleCount)
{
  string           w,words="";
  string::iterator c;
  int              i;

  // Special cases:

  if ( tuple=="0" && tupleCount==0 )
    return units[0];

  if ( tuple=="000" && tupleCount>0 )
    return "";

  for ( i=tuple.length(),c=tuple.begin() ; c<tuple.end() ; i--,c++ ) {
    w = "";
    switch ( i ) {
      case 1:
        if ( *c!='0' )
          w = getUnits(*c);
        break;
      case 2:
        if ( w!="" )
          w = w+and;
        if ( *c=='1' )
          w = getTeens(*(c+1));
        if ( *c>'1' )
          w = getTens(*c);
        break;
      case 3:
        if ( *c!='0' )
          w = getUnits(*c)+" "+hundred;
        break;
    }
    if ( w!="" )
      if ( words=="" )
        words = w;
      else
        words = words+" "+w;
  }
  if ( tupleCount>0 )
    words = words+" "+getFactors(tupleCount);

  return words;
}

// _________________________________________________________________________
string
numWords::getUnits(char index)
{
  if ( '0'<=index && index<='9' )
    return units[index-0x30];
  else
    return "?";
}

// _________________________________________________________________________
string
numWords::getTeens(char index)
{
  if ( '0'<=index && index<='9' )
    return teens[index-0x30];
  else
    return "?";
}

// _________________________________________________________________________
string
numWords::getTens(char index)
{
  if ( '0'<=index && index<='9' )
    return tens[index-0x30];
  else
    return "?";
}

// _________________________________________________________________________
string
numWords::getFactors(int index)
{
  if ( 0<=index && index<MAX_FACTORS )
    return factors[index];
  else
    return "?";
}

Commented:
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;

Commented:
One more in same method()

      case 2:
        if ( *c>='1' && words!="" )
          w = w+and;
CERTIFIED EXPERT
Author of the Year 2009

Commented:
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

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

Babu
CERTIFIED EXPERT
Author of the Year 2009

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

   About pie

-- Dan

Author

Commented:
Thaks to give me complete source code.
Sumit

Commented:
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.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.