Last active
August 29, 2015 13:55
-
-
Save kashwaa/8765620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// This class Converts Money to text in Arabic | |
/// </summary> | |
public class MoneyToText | |
{ | |
static string[] ones ={ "صفر", | |
"واحد", | |
"اثنان", | |
"ثلاثه", | |
"اربعه", | |
"خمسه", | |
"سته", | |
"سبعه", | |
"ثماينه", | |
"تسعه", | |
"عشره", | |
"احدي عشر", | |
"اثني عشر", | |
"ثلاثه عشر", | |
"اربعه عشر", | |
"خمسه عشر", | |
"سته عشر", | |
"سبعه عشر", | |
"ثمانيه عشر", | |
"تسعه عشر" }; | |
static string[] Tens = new string[] { "", | |
"عشره", | |
"عشرون", | |
"ثلاثون", | |
"اربعون", | |
"خمسون", | |
"ستون", | |
"سبعون", | |
"ثمانون", | |
"تسعون" }; | |
static string[] hundreds = new string[] { "", | |
"مائه", | |
"مائتان", | |
"ثلاثمائه", | |
"اربعمائه", | |
"خمسمائه", | |
"ستمائه", | |
"سبعمائه", | |
"ثمنمائه", | |
"تسعمائه" }; | |
/// <summary> | |
/// This method converts money to Arabic text, Example:string s = MoneyToText.convert(195985554.500M, "جنيه", "قرش", 2) return "مائه و خمسه و تسعون مليون و تسعمائه و خمسه و ثمانون الف و خمسمائه و اربعه و خمسون جنيه و خمسون قرش" | |
/// </summary> | |
/// <param name="value">The value to be converted</param> | |
/// <param name="currencyName">The name of the currency in Arabic</param> | |
/// <param name="fractionName">The name of the fraction in Arabic</param> | |
/// <param name="decimalPlaces">The number of decimal places for the currency</param> | |
/// <returns></returns> | |
public static string convert(decimal value, string currencyName, string fractionName, int decimalPlaces) | |
{ | |
string val = value.ToString(); | |
int[] valarray = new int[2]; | |
valarray[0] = Convert.ToInt32(val.Split('.')[0]); | |
if (val.Contains(".")) | |
{ | |
valarray[1] = Convert.ToInt32((value-valarray[0]).ToString(GetDecAcc(decimalPlaces)).Substring(1,decimalPlaces)); | |
} | |
else | |
{ | |
valarray[1] = 0; | |
} | |
string[] resultarr = new string[valarray[1]>0?2:1]; | |
if (valarray[0] > 0) | |
{ | |
resultarr[0] = convert(valarray[0]) + " " + currencyName; | |
} | |
if (valarray[1] > 0) | |
{ | |
resultarr[1] = convert(Convert.ToInt32(valarray[1].ToString())) + " " + fractionName; | |
} | |
return String.Join(" و ", resultarr); | |
} | |
static string convert(int value) | |
{ | |
List<string> res = new List<string>(); | |
int initialdec = GetDecs(value.ToString()); | |
while (value>0) | |
{ | |
if (initialdec == 1000000000) | |
{ | |
res.Add(getBillions(value / 1000000000)); | |
value = value % 1000000000; | |
} | |
else if (initialdec == 1000000) | |
{ | |
res.Add(getMillions(value / 1000000)); | |
value = value % 1000000; | |
} | |
else if (initialdec == 1000) | |
{ | |
res.Add(getThousands(value / 1000)); | |
value = value % 1000; | |
} | |
else if (initialdec == 100) | |
{ | |
res .Add( getHundreds(value/100)); | |
value = value % 100; | |
} | |
else if (initialdec == 10 || initialdec == 1) | |
{ | |
res .Add( getTens(value)); | |
value -= value; | |
} | |
initialdec =DecreaseDec(initialdec); | |
} | |
return string.Join(" و ",res); | |
} | |
static string getOnes(int i) | |
{ | |
return ones[i]; | |
} | |
static string getTens(int i) | |
{ | |
if (i < 20) | |
{ | |
return ones[i]; | |
} | |
else | |
{ | |
string res = ""; | |
string s = i.ToString(); | |
if (s[1] != '0') | |
res += getOnes(Convert.ToInt32(s[1].ToString())) + " و "; | |
res += Tens[(Convert.ToInt32(s[0].ToString()))]; | |
return res; | |
} | |
} | |
static string getHundreds(int i) | |
{ | |
return hundreds[i]; | |
} | |
static string getThousands(int i) | |
{ | |
if (i==1) | |
{ | |
return "الف"; | |
} | |
else if(i==2) | |
{ | |
return "الفين"; | |
} | |
else | |
{ | |
return convert(i) +(i<=10?" الاف": " الف"); | |
} | |
} | |
static string getMillions(int i) | |
{ | |
if (i == 1) | |
{ | |
return "مليون"; | |
} | |
else if (i == 2) | |
{ | |
return "مليونان"; | |
} | |
else | |
{ | |
return convert(i) + (i <= 10 ? " ملايين" : " مليون"); | |
} | |
} | |
static string getBillions(int i) | |
{ | |
if (i == 1) | |
{ | |
return "مليار"; | |
} | |
else if (i == 2) | |
{ | |
return "ملياران"; | |
} | |
else | |
{ | |
return convert(i) + (i <= 10 ? " مليارات" : " مليار"); | |
} | |
} | |
static int GetDecs(string value) | |
{ | |
string res = "1"; | |
for (int i = 0; i < value.Length-1; i++) | |
{ | |
res += "0"; | |
} | |
return Convert.ToInt32(res); | |
} | |
static string GetDecAcc(int decimals) | |
{ | |
string res = "."; | |
for (int i = 0; i < decimals; i++) | |
{ | |
res += "0"; | |
} | |
return res; | |
} | |
static int DecreaseDec(int dec) | |
{ | |
return dec / 10; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment