Converting the decimal values into words in c#


multiple functions to convert the given decimal value into words using c#, using some of the build in functions it takes integer part separtely and decimal part separetly and displays the number in words and after deciamal point

declaring a structe to store the store no and its value


public struct ConvStruct
{
public Double No;
public string Name;
}


global declaration of variables


string[] a1 = new string[20];
string[] a2 = new string[9];
ConvStruct[] a3 = new ConvStruct[4];




Declaring arrays to store the words



public void InitNumbers()
{
a1[0] = "";
a1[1] = "One";
a1[2] = "Two";
a1[3] = "Three";
a1[4] = "Four";
a1[5] = "Five";
a1[6] = "Six";
a1[7] = "Seven";
a1[8] = "Eight";
a1[9] = "Nine";
a1[10] = "Ten";
a1[11] = "Eleven";
a1[12] = "Twelve";
a1[13] = "Thirteen";
a1[14] = "Fourteen";
a1[15] = "Fifteen";
a1[16] = "Sixteen";
a1[17] = "Seventeen";

a1[18] = "Eighteen";
a1[19] = "Nineteen";
a2[0] = "";
a2[1] = "Twenty";
a2[2] = "Thirty";
a2[3] = "Forty";
a2[4] = "Fifty";
a2[5] = "Sixty";
a2[6] = "Seventy";
a2[7] = "Eighty";
a2[8] = "Ninty";
a3[0].No = 10000000;
a3[0].Name = " Crore ";
a3[1].No = 100000;
a3[1].Name = " Lakh ";
a3[2].No = 1000;
a3[2].Name = " Thousand ";
a3[3].No = 100;
a3[3].Name = " Hundred ";


}



function RupeesNPaise which takes the double value
which inturn calls two function
Convert2Words()
GetDecimal()



public string RupeesNPaise(Double Num)
{
string paise;
paise = Convert2Words(Num);
if (GetDecimal(Num) > 0)
{

paise = paise + " and " + Convert2Words(GetDecimal(Num));
return paise;
}

else
return paise;
}




public string Convert2Words(Double Num)
{
string word="";
InitNumbers();
Double a;
int b, i;
if (Num == 0)
{
word = "";
return word;
}

a = (int)Num;
for (i = 0; i < 3; i++)
{
b= (int)a / (int) a3[i].No;
if (b > 0)
word = word + InWords(b) + a3[i].Name;
a=a-(b* a3[i].No);
word = word + InWords(a);

}

return word;
}



public int GetDecimal(Double Number)
{
int getdecimal;

getdecimal=Convert.ToInt16( string.Format("{0:#######0.000}", Number).Substring(string.Format("{0:#######0.000}", Number).IndexOf(".") + 1));
return getdecimal;





}

public string InWords(Double No)
{
string inwords;
int b,c;
if (No > 19)
{
b = ((int)(No / 10));
c = ((int)(No - (b * 10)));
inwords = a2[b - 1] + a1[c];
return inwords;
}
else
{
inwords = a1[(int)No];
return inwords;
}
}


Comments

Guest Author: kohinoor15 Feb 2013

Error in code

inwords = a2[b - 1] + a1[c];

Index was outside the bounds of the array.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: