Scroll To Top
Reader Level:
Article
Current Affairs

Number to Currency Convertor

By Murlidharan S on Sep 03, 2001
The attached Num2Cur.cs file converts number to currency up to 999,999,999,999.9999 i.e (999 billion).

The attached Num2Cur.cs file converts number to currency up to 999,999,999,999.9999 i.e (999 billion).

using System;
using System.Windows.Forms;
using System.Drawing;
class frm1
{
TextBox txtNo;
Label lblDisplay,lblText;
Button cmdClick;
Form frmNo2Cur;
int Hun,Tho,Mil,Bil;
String[] Ones =
new String[] {"","One","Two",
"Three","Four","Five","Six","Seven","Eight","Nine"};
public frm1()
{
frmNo2Cur=
new Form();
cmdClick=
new Button();
cmdClick.Text="Click";
cmdClick.Location =
new Point(300,60);
frmNo2Cur.Controls.Add(cmdClick);
txtNo=
new TextBox();
txtNo.Location =
new Point(130,60);
txtNo.Width =150;
frmNo2Cur.Controls.Add(txtNo);
lblDisplay=
new Label();
lblDisplay.Location =
new Point(20,60);
lblDisplay.Text="Enter the Number";
frmNo2Cur.Controls.Add(lblDisplay);
lblText=
new Label();
lblText.Location =
new Point(10,100);
lblText.Width=500;
lblText.Height=500;
frmNo2Cur.Controls.Add(lblText);
cmdClick.Click +=
new EventHandler(cmdClick_Click);
frmNo2Cur.Width=550;
frmNo2Cur.Text="Number to Currency";
frmNo2Cur.ShowDialog();
}
void Divide(String strn1)
{
int n1=strn1.Length;
switch(n1)
{
case 1:
goto case 3;
case 2:
goto case 3;
case 3:
Hun=Int32.Parse(strn1);
break;
case 4:
goto case 6;
case 5:
goto case 6;
case 6:
Hun=Int32.Parse(strn1.Substring(n1-3,3));
Tho=Int32.Parse(strn1.Substring(0,n1-3));
break;
case 7:
goto case 9;
case 8:
goto case 9;
case 9:
Hun=Int32.Parse(strn1.Substring(n1-3,3));
Tho=Int32.Parse(strn1.Substring(n1-6,3));
Mil=Int32.Parse(strn1.Substring(0,n1-6));
break;
case 10:
goto case 12;
case 11:
goto case 12;
case 12:
Hun=Int32.Parse(strn1.Substring(n1-3,3));
Tho=Int32.Parse(strn1.Substring(n1-6,3));
Mil=Int32.Parse(strn1.Substring(n1-9,3));
Bil=Int32.Parse(strn1.Substring(0,n1-9));
break;
}
}
string SplitDigits(int n2,String mode)
{
String[] Teens=
new String []
"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eightteen","Nineteen"};
String[] Tys=
new String[]{"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
String strH="",strT="";
int hun1=0,ten1=0,one1=0;
hun1=n2 /100;
if(hun1>0)
{strH= Ones[hun1] + " Hundred ";}
else if (hun1==0)
{strH=Ones[hun1];}
ten1=n2 % 100;
if (ten1>9 && ten1<20)
{
strT=Teens[ten1 % 10];}
else if (ten1>=20 || ten1<10)
{
one1=ten1 % 10;
ten1=ten1/10;
if (one1==0)
{
strT=Tys[ten1];
}
else
{
strT= Tys[ten1]+ " " +Ones[one1] ;
}
}
return (strH+strT);
}
string SplitDigits(String n2)
{
String strFra="";
int cnt=0;
for (int i=0;i<n2.Length;i++)
{
cnt=Int32.Parse(n2.Substring(i,1));
if (cnt==0)
strFra+=" zero ";
strFra+=" " + Ones[cnt];
}
return strFra;
}
string GenerateString(String strTxt)
{
String Num=""; String Fra="";
String strHun="",strTho="",strMil="",strBil="",strFra="";
try
{
if ( strTxt.IndexOf(".")<0)
{
Num=strTxt;
}
else
{
Num=strTxt.Substring(0,strTxt.IndexOf("."));
Fra=strTxt.Substring(strTxt.IndexOf(".")+1);
}
if (Num!="")
{Divide(Num);}
if (Hun>0)
{strHun=SplitDigits(Hun,"N");}
if (Tho>0)
{strTho=SplitDigits(Tho,"N")+" Thousand and ";}
if (Mil>0)
{strMil=SplitDigits(Mil,"N")+" Million and ";}
if (Bil>0)
{strBil=SplitDigits(Bil,"N")+ " Billion and " ;}
if (Fra!="")
{strFra=" point " + SplitDigits(Fra);}
}
catch(FormatException)
{
MessageBox.Show("It is not a Number","Error");
strBil="";strMil="";strTho="";strHun="";strFra="";
}
catch(OverflowException)
{
MessageBox.Show("The number is too large or too small","Error");
}
return (strBil + strMil + strTho + strHun +strFra );
}
protected void cmdClick_Click(Object sender , EventArgs e)
{
lblText.Text=GenerateString(txtNo.Text);
}
public static void Main(string []s)
{
frm1 frm=
new frm1();
}
}