Sunday, June 13, 2010

.Net Class for convert Numbers to Arabic Characters - تفقيط

I think that this task is common on all finance related softwares, we always need to print numbers in words at end of some report, so i am listing below a C# class for doing so, i t will take 3 parameters:

1- double theNo : Numbers to be converted to characters along with fraction if needed.
2- string mainCurr : The main currency name such as Dinar.
3- string subCurr : Sub currency name, used for fractions.

----------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Global
{
class NumericToChar
{
public static string NumberToWord_Ar(double theNo, string mainCurr, string subCurr)
{
string NoToTxt = String.Empty;
string[] MyArry1 = new string[9 + 1];
string[] MyArry2 = new string[9 + 1];
string[] MyArry3 = new string[9 + 1];
string Myno = String.Empty;
string GetNo = String.Empty;
string RdNo = String.Empty;
string My100 = String.Empty;
string My10 = String.Empty;
string My1 = String.Empty;
string My11 = String.Empty;
string My12 = String.Empty;
string GetTxt = String.Empty;
string Mybillion = String.Empty;
string MyMillion = String.Empty;
string MyThou = String.Empty;
string MyHun = String.Empty;
string MyFraction = String.Empty;
string MyAnd = String.Empty;
int I = 0;
string ReMark = String.Empty;

if (theNo > 999999999999.999)
{
return NoToTxt;
}

if (theNo <>
{
theNo = theNo * -1.0;
ReMark = "يتبقى لكم ";
}
else
{
ReMark = " ";
}

if (theNo == 0.0)
{
NoToTxt = "صفر";
return NoToTxt;
}

MyAnd = " و ";
MyArry1[0] = "";
MyArry1[1] = "مائة";
MyArry1[2] = "مائتان";
MyArry1[3] = "ثلاثمائة";
MyArry1[4] = "أربعمائة";
MyArry1[5] = "خمسمائة";
MyArry1[6] = "ستمائة";
MyArry1[7] = "سبعمائة";
MyArry1[8] = "ثمانمائة";
MyArry1[9] = "تسعمائة";

MyArry2[0] = "";
MyArry2[1] = " عشر";
MyArry2[2] = "عشرون";
MyArry2[3] = "ثلاثون";
MyArry2[4] = "أربعون";
MyArry2[5] = "خمسون";
MyArry2[6] = "ستون";
MyArry2[7] = "سبعون";
MyArry2[8] = "ثمانون";
MyArry2[9] = "تسعون";

MyArry3[0] = "";
MyArry3[1] = "واحد";
MyArry3[2] = "اثنان";
MyArry3[3] = "ثلاثة";
MyArry3[4] = "أربعة";
MyArry3[5] = "خمسة";
MyArry3[6] = "ستة";
MyArry3[7] = "سبعة";
MyArry3[8] = "ثمانية";
MyArry3[9] = "تسعة";
// ======================
GetNo = theNo.ToString("000000000000.000");

I = 0;
while (I <>
{

if (I <>
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(GetNo, I + 1, 3)
Myno = GetNo.Substring(I + 1 - 1, 3);
}
else
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(GetNo, I + 2, 2)
Myno = "0" + GetNo.Substring(I + 2 - 1, 2);
}

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1 - 1, 3)), "0") > 0)
{

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
RdNo = Myno.Substring(1 - 1, 1);
My100 = MyArry1[Convert.ToInt32(RdNo)];
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
RdNo = Myno.Substring(3 - 1, 1);
My1 = MyArry3[Convert.ToInt32(RdNo)];
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
RdNo = Myno.Substring(2 - 1, 1);
My10 = MyArry2[Convert.ToInt32(RdNo)];

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 11)
{
My11 = "إحدى عشر";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 12)
{
My12 = "إثنى عشر";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 10)
{
My10 = "عشرة";
}

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 2)
if ((String.Compare((Myno.Substring(1 - 1, 1)), "00") > 0) && (String.Compare((Myno.Substring(2 - 1, 2)), "00") > 0))
{
My100 = My100 + MyAnd;
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
if ((String.Compare((Myno.Substring(3 - 1, 1)), "0") > 0) && (String.Compare((Myno.Substring(2 - 1, 1)), "1") > 0))
{
My1 = My1 + MyAnd;
}

GetTxt = My100 + My1 + My10;

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
if (((Myno.Substring(3 - 1, 1)) == "1") && ((Myno.Substring(2 - 1, 1)) == "1"))
{
GetTxt = My100 + My11;
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
if (((Myno.Substring(1 - 1, 1)) == "0"))
{
GetTxt = My11;
}
}

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 2, 1)
if (((Myno.Substring(3 - 1, 1)) == "2") && ((Myno.Substring(2 - 1, 1)) == "1"))
{
GetTxt = My100 + My12;
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 1)
if (((Myno.Substring(1 - 1, 1)) == "0"))
{
GetTxt = My12;
}
}

if ((I == 0) && (GetTxt != ""))
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1 - 1, 3)), "10") > 0)
{
Mybillion = GetTxt + " مليار";
}
else
{
Mybillion = GetTxt + " مليارات";
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "2"))
{
Mybillion = " مليار";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "2"))
{
Mybillion = " ملياران";
}
}
}

if ((I == 3) && (GetTxt != ""))
{

// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1 - 1, 3)), "10") > 0)
{
MyMillion = GetTxt + " مليون";
}
else
{
MyMillion = GetTxt + " مليون";
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "1"))
{
MyMillion = " مليون";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (((Myno.Substring(1 - 1, 3)) == "2"))
{
MyMillion = " مليونان";
}
}
}

if ((I == 6) && (GetTxt != ""))
{
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 1, 3)
if (String.Compare((Myno.Substring(1, 2)), "02") <= 0)
{
MyThou = GetTxt + " ألف";
}
else
{
MyThou = GetTxt + " ألف";
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
if (((Myno.Substring(3 - 1, 1)) == "1") && (Convert.ToInt32(Myno.Substring(2 - 1, 2)) != 11))
{
MyThou = " ألف";
}
// ISSUE: Potential Substring problem; VB6 Original: Mid$(Myno, 3, 1)
else if (((Myno.Substring(3 - 1, 1)) == "2") && (Convert.ToInt32(Myno.Substring(2 - 1, 2)) == 12))
{
MyThou = " ألفان";
}
else if (Convert.ToInt32(Myno.Substring(2 - 1, 2)) <= 10)
{
MyThou = GetTxt + " آلاف";
}
}
}

if ((I == 9) && (GetTxt != ""))
{
MyHun = GetTxt;
}
if ((I == 12) && (GetTxt != ""))
{
MyFraction = Myno;
}
}

I = I + 3;
}

if ((Mybillion != ""))
{
if ((MyMillion != "") || (MyThou != "") || (MyHun != ""))
{
Mybillion = Mybillion + MyAnd;
}
}

if ((MyMillion != ""))
{
if ((MyThou != "") || (MyHun != ""))
{
MyMillion = MyMillion + MyAnd;
}
}

if ((MyThou != ""))
{
if ((MyHun != ""))
{
MyThou = MyThou + MyAnd;
}
}
if (theNo.ToString().IndexOf(".") != -1)
MyFraction = theNo.ToString("0.000").Substring(theNo.ToString("0.000").IndexOf(".") + 1);

if (MyFraction != "")
{
if ((Mybillion != "") || (MyMillion != "") || (MyThou != "") || (MyHun != ""))
{
NoToTxt = ReMark + Mybillion + MyMillion + MyThou + MyHun + " " + mainCurr + MyAnd + MyFraction + " " + subCurr;
}
else
{
NoToTxt = ReMark + MyFraction + " " + subCurr;
}
}
else
{
NoToTxt = ReMark + Mybillion + MyMillion + MyThou + MyHun + " " + mainCurr;
}

return NoToTxt;
}

}
}

-----------------------------------------------------------------------------------------------