Code for convertin num to words

Status
Not open for further replies.

sms_solver

In the zone
Can somebody provide the code or link to convert numbers into words
eg: 5322--> Five Thousand, Three Hundred and Twenty Two

I am asking this, because I have made a class for this kind of conversion in VB6. My code is nothing more than 90 lines.

I want to compare mine code with some ones others.
 
OP
sms_solver

sms_solver

In the zone
the important point is logic (or say algorithm). Your logic can be implemented in some programming language in less line of code then other programming language.

I just want to know other logic to compare with mine.

I have made this converting code in C, VB, Delphi and C#.
 

tuxfan

Technomancer
Can you post your VB/C code? I had made this type of code in COBOL and BASIC :shock: :) many years back.
 

Thor

Ambassador of Buzz
I hv done it in C++ :
Code:
//A program to convert Number into Words
// Works Upto 9 Billion ...
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
char d1[][10]={" ","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",};
char d2[][10]={" ", "Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
char d3[][15]={"","Hundred and","Thousand and","Lakh and","Million and","Billion and"};
void test(long int);
void ten(long int);
void hun(long int);
void thou(long int);
void lakh(long int);
void mill(long int);
void bill(long int);
void main()
{
 long int num;
 cout<<"Enter the Number to be turned into Words : \n";
 cout<<"Remember Do Not Enter Zero at front\nbecause C++ shall treat it as Octal No.\n\n\n\t\t\tThe Number\t:";
 scanf("%ld",&num);
 cout<<"\nIn Words :\n";
 if (num<0)
  {
   cout<<"Minus ";
   num=-num;
  }
 test(num);
 getch();
}

void test(long int num)
 {
  long int copy=num;
  if (num<20)
  ten(copy);
  if (num>=20 && num <=99)
   ten(copy);
  if (num>=100 && num <=999)
   hun(copy);
  if (num>=1000 && num<100000)
   thou(copy);
  if (num>=100000 && num<=9999999)
   lakh(num);
  if (num>=10000000 && num<=999999999)
   mill(num);
  if (num>=1000000000)
   bill(num);
 }

void ten(long int copy)
 {
  if (copy>=0 && copy <20)
  cout<<d1[copy]<<" ";
  else
  cout<<d2[copy/10]<<" "<<d1[copy%10]<<" ";
  return;
 }
void hun(long int copy)
 {
  cout<<d1[copy/100]<<" "<<d3[1]<<" ";
  test(copy%100);
  return;
 }

void thou(long int copy)
 {
  test(copy/1000);
  cout<<d3[2]<<" ";
  test(copy%1000);
  return;
 }
void lakh(long int copy)
 {
  test(copy/100000);
  cout<<d3[3]<<" ";
  test(copy%100000);
  return;
 }
 void mill(long int copy)
 {
  test(copy/10000000);
  cout<<d3[4]<<" ";
  test(copy%10000000);
  return;
 }
 void bill(long int copy)
 {
  test(copy/1000000000);
  cout<<d3[5]<<" ";
  test(copy%1000000000);
  return;
 }



Now check this code and run it :
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
 long a,b,c=0;
 cout<<"A  ? = ";
 cin>>a;
 cout<<"Therefore A = "<<a<<endl;
 cout<<"\nB ? = ";
 cin>>b;
 cout<<"Therefore B = "<<b<<endl;
 getch();
}

Funny huh ?
Just found out , that When inputs lik 023 or 012 is given as input in long type variables ,it shall be treated as Octal no.
Therefore : 012 becomes 10 and 010 becomes 8 !!
 

hafees

In the zone
here is my C code
Code:
/************************************************************************
					Programming by Hafees (hafees@msn.com).
			 program to accept amount and to print it in words
			Can convert any number between -MAXLONG to +MAXLONG.
					i.e -214,74,83,647 to +214,74,83,647
**************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<values.h>
void printsingles(int);
void printtenths(int);
void printother(int);

int main()
{
	long int num,left;
	int tens,hundreds,thousands;
	clrscr();
	printf("Enter an Integer Number [Maximum %ld ] ",MAXLONG);
	scanf("%ld",&num);
	printf("\n\n");
	if(num<0) //if negative
	{
		printf("Minus");
		left=num* (-1); //convert into +ve
	}
	else
		left=num;
	do
	{
		if(left<20)
		{
			printsingles(left);
			left=0;
		}
		else if(left<100)
		{
			tens=left/10;
			printtenths(tens*10);
			left-=(tens*10);
		}

		else if(left<1000)
		{
			hundreds=left/100;
			printsingles(hundreds);
			printf(" Hundred");
			left-=(hundreds*100);
			if(left>0) printf(" and");
		}
		else if (left<10000)
		{
			thousands=left/1000;
			printsingles(thousands);
			printf(" Thousand");
			left-=(thousands*1000);
			if(left>0) printf(" and");
		}
		else if (left<100000L)
		{
			thousands=left/1000;
			printother(thousands);
			printf(" Thousand");

			left-=(long)(thousands*1000L);
			if(left>0) printf(" and");
		}
		else if (left<10000000L)
		{
			int lacks;
			lacks=left/100000L;
			printother(lacks);
			printf(" Lacks");
			left-=(long)(lacks*100000L);
			if(left>0) printf(" and");
		}
		else if (left<=MAXLONG)
		{
			int crores;
			crores=left/10000000L;
			printother(crores);
			printf(" Crores");
			left-=(long)(crores*10000000L);
			if(left>0) printf(" and");
		}
		else
		{
			printf("Unable to convert the number %ld into words",num);
			break;
		}

	}
	while(left);
	getch();
	return 0;
}


void printsingles(int num)
{
	switch(num)
	{
		case 0: printf(" Zero");break;
		case 1: printf(" One");break;
		case 2: printf(" Two");break;
		case 3: printf(" Three");break;
		case 4: printf(" Four");break;
		case 5: printf(" Five");break;
		case 6: printf(" Six");break;
		case 7: printf(" Seven");break;
		case 8: printf(" Eight");break;
		case 9: printf(" Nine");break;
		case 10: printf(" Ten");break;
		case 11: printf(" Eleven");break;
		case 12: printf(" Tweleve");break;
		case 13: printf(" Thirteen");break;
		case 14: printf(" Fourteen");break;
		case 15: printf(" Fifteen");break;
		case 16: printf(" Sixteen");break;
		case 17: printf(" Seventeen");break;
		case 18: printf(" Eighteen");break;
		case 19: printf(" Ninteen");
	}

}

void printtenths(int tens)
{
	switch(tens)
	{
	case 20: printf(" Twenty");break;
	case 30: printf(" Thirty");break;
	case 40: printf(" Forty");break;
	case 50: printf(" Fifty");break;
	case 60: printf(" Sixty");break;
	case 70: printf(" Seventy");break;
	case 80: printf(" Eighty");break;
	case 90: printf(" Ninty");break;
	}
}

void printother(int t)
{
	int tens;
	if(t<20)
	{
		printsingles(t);

	}
	else if(t<100)
	{
		tens=t/10;
		printtenths(tens*10);
		t-=(tens*10);
		if(t>0) printsingles(t);
	}
	else if(t<1000) //when number > 999999999
	{
		int hundreds;
		hundreds=t/100;
		printsingles(hundreds);
		printf(" Hundred");
		t-=(hundreds*100);
		if(t>0) printf(" and");
		printother(t); //call function once again (recursive calling)
					   // to print ther remaining which is  less than 100.
	}
}
 
OP
sms_solver

sms_solver

In the zone
I am giving my Visual Basic 6 class to convert numbers into words. It can be made Active X Dll and can be used for development. It works even for very large number!

Working Principle:
It works by analyzing group of 3 numbers. the function hundred in common and called for each group of numbers.

Code:
Private tString As String
Private mlen As Byte

Private Ones(0 To 9) As String
Private Tens(2 To 9) As String
Private FixTen(10 To 19) As String
Private MoneyVal(0 To 9) As String

Private Sub Class_Initialize()
    Ones(0) = ""
    Ones(1) = "One":    Ones(2) = "Two":    Ones(3) = "Three"
    Ones(4) = "Four":   Ones(5) = "Five":   Ones(6) = "Six"
    Ones(7) = "Seven":  Ones(8) = "Eight":  Ones(9) = "Nine"
    
    Tens(2) = "Twenty ": Tens(3) = "Thirty "
    Tens(4) = "Forty ": Tens(5) = "Fifty "
    Tens(6) = "Sixty ": Tens(7) = "Seventy "
    Tens(8) = "Eighty ": Tens(9) = "Ninty "
    
    FixTen(10) = "Ten": FixTen(11) = "Eleven": FixTen(12) = "Twelve"
    FixTen(13) = "Thirteen": FixTen(14) = "Fourteen": FixTen(15) = "Fifteen"
    FixTen(16) = "Sixteen": FixTen(17) = "Seventeen": FixTen(18) = "Eighteen"
    FixTen(19) = "Nineteen"
    
    MoneyVal(0) = "": MoneyVal(1) = "Thousand, "
    MoneyVal(2) = "Million, ": MoneyVal(3) = "Billion, "
    MoneyVal(4) = "Trillion, ": MoneyVal(5) = "Quadrillion, "
    MoneyVal(6) = "Quintillion, ": MoneyVal(7) = "Sextillion, "
    MoneyVal(8) = "Septillion, ": MoneyVal(9) = "Octillion, "
End Sub

Public Function Convert(inp As String) As String
Dim xyz As String
Dim Q As Byte, R As Byte
Dim NN As Integer
Dim x As Byte
Dim UPR As Byte

    mlen = Len(inp)
    x = 1
    Q = mlen \ 3    'impo
    R = mlen Mod 3
    
    If R = 0 Then UPR = Q Else UPR = Q + 1
    
    For NN = UPR To 1 Step -1
        If NN = UPR And R > 0 Then
           ' MsgBox Mid(inp, 1, R)
            xyz = Hundred(Mid(inp, 1, R)) & " " & MoneyVal(NN - 1)
        Else
           ' MsgBox "X: " & x & Mid(inp, R + x, 3)
            xyz = xyz & Hundred(Mid(inp, R + x, 3)) & " " & MoneyVal(NN - 1)
            x = x + 3
        End If
    Next
    
Convert = xyz
End Function

Private Function Hundred(inp As String) As String
Dim mVal As Byte
Dim tmp As String
Dim xString As String * 1, yString As String * 1

mlen = Len(inp)
If mlen > 3 Then Exit Function

If mlen = 3 Then
    tString = Mid(inp, 1, 1)
    tmp = Ones(Val(tString)) & " Hundred "
End If

'Common to all
If mlen = 3 Then tString = Mid(inp, 2) Else tString = inp   'impo
    
    mVal = Val(tString)
    If mVal >= 0 And mVal < 10 Then
        tmp = tmp & Ones(mVal)
    ElseIf mVal > 9 And mVal < 20 Then
        tmp = tmp & FixTen(mVal)
    Else
        If mlen = 3 Then xString = Mid(inp, 2, 1) Else xString = Mid(inp, 1, 1)
        If mlen = 3 Then yString = Mid(inp, 3, 1) Else yString = Mid(inp, 2, 1)
        tmp = tmp & Tens(Val(xString)) & Ones(Val(yString))
    End If
Hundred = tmp
End Function
 

Thor

Ambassador of Buzz
I hv modified My code to now Accept -ve numbers .
And now "and" is put between words !!
 
Status
Not open for further replies.
Top Bottom