using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WindowsFormsApplication1 { public class clsCurrency { public static string GetNumericExpression(decimal prmNumber, RoundOption prmRoundOption) { //prmRoundOption --> يستخدم لتحديد نوع التقريب بالمليم أو بالقرش أو بالجنيه if (prmNumber < 0 || prmNumber > 999999999999999) { return "الرقم المكتوب غير مدعوم"; } int RoundPlaces = prmRoundOption == RoundOption.جنيه ? 0 : prmRoundOption == RoundOption.قرش ? 2 : 3; string TheFormatting = ",000,000,000,000,000.000", FractionCoinSingle = "قرش", FractionCoinPlural = "قروش"; if (prmRoundOption == RoundOption.مليم) { FractionCoinSingle = "مليم"; FractionCoinPlural = "مليمات"; } prmNumber = decimal.Round(prmNumber, RoundPlaces); //Converting the decimal into string divided by comma every three digits. //and converting the '.' to ',' so we have 6 parts, the sixth one is the decimal part. string Number = prmNumber.ToString(TheFormatting).Replace('.', ','); //قرش string LowerThree = prmNumber == 0 ? "لا شىء" : prmNumber == 1 ? "جنيه واحد" : prmNumber == 2 ? "جنيهان" : (prmNumber == 0.01m && prmRoundOption == RoundOption.قرش) ? "قرش واحد" : (prmNumber == 0.02m && prmRoundOption == RoundOption.قرش) ? "قرشان" : (prmNumber == 0.001m && prmRoundOption == RoundOption.مليم) ? "مليم واحد" : (prmNumber == 0.002m && prmRoundOption == RoundOption.مليم) ? "مليمان" : ""; if (LowerThree != "") { return LowerThree; } //Divid the string into 5 parts each of three digits. string[] AllParts = Number.Split(','); //Will contain the final expression string FinalPound = "", FinalMalim = ""; //loop through the 5 parts, starting from the last one for (int i = AllParts.Length - 1; i >= 0; i--) { //Each part of them have its own word, starting with no thing then 'ألف', and so on string Major = i == 5 ? FractionCoinSingle : i == 4 ? "جنيه" : i == 3 ? "ألف" : i == 2 ? "مليون" : i == 1 ? "مليار" : i == 0 ? "تريليون" : ""; //Getting every part to get its expression. string CurrentTriple = AllParts[i]; //السطر التالى لإضافة صفراً فى خانة المئات للجزء العشرى إذا العمل بنظام القروش if (prmRoundOption == RoundOption.قرش && i == 5) { CurrentTriple = "0" + CurrentTriple; } //If the part is '000', so ignore and continue to the next part. if (CurrentTriple == "000") { continue; } //Minor[0] --> Handreds مئات //Minor[1] --> Tens عشرات //Minor[2] --> numbers آحاد string[] Minor = { "", "", "" }; //Loop through the current part extracting its three digits one by one to //know its expression alone and then concatenat them in one string. for (int j = 0; j < 3; j++) { string CurrentDigit = CurrentTriple[j].ToString(); byte prmN = byte.Parse(CurrentDigit); //If 0 so continue, expression of this digit is empty. if (prmN == 0) { Minor[j] = ""; continue; } //Next line to multiply every digit in corresponding position, i.e. 100, 10 or 1. //If j = 0, so we extract the handreds, so we need to multiply the extractex number //by 100 which is [10^(2-j)] --> 10^(2-0) = 100..and so on. int N = (prmN * (int)Math.Pow(10, (2 - j))); //Using the enum to retun the word corresponding to the number sent, and save it in Minor[j]. Minor[j] = ((Words)N).ToString(); } //We insert the "و" according the returned expression. //AndAfter100 --> between handreds and Numbers بين المئات والآحاد //AndAfter10 --> between Numbers and Tens بين الآحاد والعشرات //لاحظ أن فى كتابة الأرقام ننطق المئات ثم الآحاد ثم العشرات string AndAfter100 = "", AndAfter10 = "", AndAfter1 = ""; //إذا وجدت المئات ووجد أى من الآحاد أو العشرات فإننا نحتاج الوار بعد نطق المئات if (Minor[0] != "" && (Minor[1] != "" || Minor[2] != "")) { AndAfter100 = " و "; } //إذا وجد الآحاد والعشرات نحتاج الواو بعد نطق (كتابة) الآحاد if (Minor[1] != "" && Minor[2] != "") { AndAfter1 = " و "; } //الجزء التالى حتى لا يكتب اثنان ألف أو اثنان مليون ولكن يكتب ألفان أو مليونان //كذلك فى حالة الألف أو المليون الواحد يحذف كلمة واحد وينطقها ألف جنيه أو مليون جنيه //بدلاً من واحد ألف جنيه أو واحد مليون جنيه if (Minor[0] == "" && Minor[1] == "") { //لا نطبق هذه العملية على الحلقة الأولى لأننا نريد تطبيقها على الألف والمليون والمليار والتريليون //أما الجزء الأول قبل الألف تظل الكلمات موجودة if (i != 4) { Major = Minor[2] == "اثنان" ? Major + "ان" : Major; Minor[2] = (Minor[2] == "اثنان" || Minor[2] == "واحد") ? "" : Minor[2]; } } //تميير ألفاظ العقود (100 و 1000 و 10000 , ...) يكون مجروراً لذلك يجب استبعادهم من التنوين //بمعنى إذا وجدت المئات فقط وكان الآحاد والعشرات فارغاً لا يدخل else if (!(Minor[0] != "" && Minor[1] == "" && Minor[2] == "")) { //الشرط التالى لاستبعاد المثنى لا ينونه Major = (!Major.EndsWith("ان")) ? Major + "اً" : Major; } //حتى لا يطبق الجمع على رقمى واحد واثنين //نريد تطبيق الجمع على الأرقام من 3-9 وكذلك رقم 10 فقط أى ليس 11 أو 12 أو ..19 if ((CurrentTriple.StartsWith("00") && !"واحداثنان".Contains(Minor[2])) || CurrentTriple == "010") { Major = Major.Contains(FractionCoinSingle) ? FractionCoinPlural : Major.Contains("جنيه") ? "جنيهات" : Major.Contains("ألف") ? "آلاف" : Major.Contains("مليون") ? "ملايين" : Major.Contains("مليار") ? "مليارات" : "تريليونات"; } //فى حالة الرقمين 11، 12 نستبدل كلمة واحد عشرة بـ أحد عشر وكلمة اثنان عشرة بـ اثنا عشر if (Minor[1] == "عشرة" && Minor[2] != "") { Minor[1] = "عشر"; Minor[2] = Minor[2] == "واحد" ? "أحد" : Minor[2] == "اثنان" ? "اثنا" : Minor[2]; AndAfter1 = " "; } AndAfter10 = FinalPound == "" ? "" : " و "; String CurrentTripleExpression = Minor[0] + AndAfter100 + Minor[2] + AndAfter1 + Minor[1] + " " + Major + AndAfter10; if (i != 5) { FinalPound = CurrentTripleExpression + FinalPound; } else { FinalMalim = CurrentTripleExpression; } } if (FinalPound.Trim() != "" && AllParts[4] == "000") { FinalPound = FinalPound.EndsWith("اً") ? FinalPound.Substring(0, FinalPound.Length - 2) : FinalPound; FinalPound = FinalPound + " جنيه"; } FinalPound = FinalPound + ((FinalMalim == "" || FinalPound == "") ? "" : " و ") + FinalMalim; return FinalPound; } enum Words { واحد = 1, اثنان = 2, ثلاثة = 3, أربعة = 4, خمسة = 5, ستة = 6, سبعة = 7, ثمانية = 8, تسعة = 9, عشرة = 10, عشرون = 20, ثلاثون = 30, أربعون = 40, خمسون = 50, ستون = 60, سبعون = 70, ثمانون = 80, تسعون = 90, مائة = 100, مائتان = 200, ثلاثمائة = 300, أربعمائة = 400, خمسمائة = 500, ستمائة = 600, سبعمائة = 700, ثمانمائة = 800, تسعمائة = 900 } public enum RoundOption { مليم, قرش, جنيه } } }