Click here to Skip to main content
Click here to Skip to main content

Convert Arabic Number To Alphabet (التفقيط) تحويل الأرقام إلى حروف

, 11 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
تفقيط الأرقام العربيه

Introduction  

Convert any number consisting of digits (from one digit to six digits) into Arabic alphabets. Also it handles "."

Background

The basic idea is to divide the number into two separeted parts if it contains "." and take every part as a number  then convert this number to alphabet by dividing it to digits.

Using the code

You just need to use the class called ConvertNumbersToArabicAlphabet and you should pass a number consisting of six digits or less to its constructor.

 //
//Use This class directly if you want to convert numbers to Alphabet but feel free to modify any mehod
//

* I think it will be better if you download the version of this class 'attached'
because it has an arabic charcter which will not be displayed right if I write it here
 class ConvertNumbersToArabicAlphabet
    {
        string NumberAr;
       public  ConvertNumbersToArabicAlphabet(string Number)
       {
         NumberAr=  ConvertNumberToAlpha(Number);
       }
        private string ConvertNumberToAlpha(string Number)
        {
            if (Number.Contains('.'))
            {
                if (Number.Split('.')[0].ToCharArray().Length > 6)
                {
                    return "No Number";
                }
                else
                {
                    switch (Number.Split('.')[0].ToCharArray().Length)
                    {
                        case 1: return convertOneDigits(Number.ToString()) + " جنيها " + " و " + convertTwoDigits(Number.Split('.')[1]) + " قرشا ";
                        case 2: return convertTwoDigits(Number.ToString()) + " جنيها " + " و " + convertTwoDigits(Number.Split('.')[1]) + " قرشا ";
                        case 3: return convertThreeDigits(Number.ToString()) + " جنيها " + " و " + convertTwoDigits(Number.Split('.')[1]) + " قرشا ";
                        case 4: return convertFourDigits(Number.ToString()) + " جنيها " + " و " + convertTwoDigits(Number.Split('.')[1]) + " قرشا ";
                        case 5: return convertFiveDigits(Number.ToString()) + " جنيها " + " و " + convertTwoDigits(Number.Split('.')[1]) + " قرشا ";
                        case 6: return convertSixDigits(Number.ToString()) + " جنيها " + " و " + convertTwoDigits(Number.Split('.')[1]) + " قرشا ";
                        default: return "";
                    }
                }
            }
            else
            {
                if (Number.ToCharArray().Length > 6)
                {
                    return "No Number";
                }
                else
                {
                    switch (Number.ToCharArray().Length)
                    {
                     case 1: return convertOneDigits(Number.ToString())    + " جنيها ";
                        case 2: return convertTwoDigits(Number.ToString()) + " جنيها ";
                        case 3: return convertThreeDigits(Number.ToString()) + " جنيها ";
                        case 4: return convertFourDigits(Number.ToString()) + " جنيها " ;
                        case 5: return convertFiveDigits(Number.ToString()) + " جنيها " ;
                        case 6: return convertSixDigits(Number.ToString()) + " جنيها " ;
                        default: return "";
                    }
                    
                    }
            }
        }
        private string convertTwoDigits(string TwoDigits)
        {
            string returnAlpha = "00";
            if (TwoDigits.ToCharArray()[0] == '0' && TwoDigits.ToCharArray()[1] != '0')
            {
                return convertOneDigits(TwoDigits.ToCharArray()[1].ToString());
            }
            else
            {
                switch (int.Parse(TwoDigits.ToCharArray()[0].ToString()))
                {
                    case 1:
                        {
                            if (int.Parse(TwoDigits.ToCharArray()[1].ToString()) == 1)
                            {
                                return "إحدى عشر";
                            }
                            else if (int.Parse(TwoDigits.ToCharArray()[1].ToString()) == 2)
                            {
                                return "إثنى عشر";
                            }
                            else
                            {
                                returnAlpha = "عشر";
                                return convertOneDigits(TwoDigits.ToCharArray()[1].ToString()) + " " + returnAlpha;
                            }
                        }
                    case 2: returnAlpha = "عشرون"; break;
                    case 3: returnAlpha = "ثلاثون"; break;
                    case 4: returnAlpha = "أريعون"; break;
                    case 5: returnAlpha = "خمسون"; break;
                    case 6: returnAlpha = "ستون"; break;
                    case 7: returnAlpha = "سبعون"; break;
                    case 8: returnAlpha = "ثمانون"; break;
                    case 9: returnAlpha = "تسعون"; break;
                    default: returnAlpha = ""; break;
                }
            }
            if (convertOneDigits(TwoDigits.ToCharArray()[1].ToString()).Length == 0)
            { return returnAlpha; }
            else
            {
                return convertOneDigits(TwoDigits.ToCharArray()[1].ToString()) + " و " + returnAlpha;
            }
        }
        private string convertOneDigits(string OneDigits)
        {
            switch (int.Parse(OneDigits))
            {
                case 1: return "واحد";
                case 2: return "إثنان";
                case 3: return "ثلاثه";
                case 4: return "أربعه";
                case 5: return "خمسه";
                case 6: return "سته";
                case 7: return "سبعه";
                case 8: return "ثمانيه";
                case 9: return "تسعه";
                default: return "";
            }
        }
        private string convertThreeDigits(string ThreeDigits)
        {
            switch (int.Parse(ThreeDigits.ToCharArray()[0].ToString()))
            {
                case 1:
                    {
                        if (int.Parse(ThreeDigits.ToCharArray()[1].ToString()) == 0)
                        {
                            if (int.Parse(ThreeDigits.ToCharArray()[2].ToString()) == 0)
                            {
                                return "مائه";
                            }
                            return "مائه" + " و " + convertOneDigits(ThreeDigits.ToCharArray()[2].ToString());
                        }
                        else
                        {
                            return "مائه" + " و " + convertTwoDigits(ThreeDigits.Substring(1, 2));
                        }
                    }
                case 2:
                    {
                        if (int.Parse(ThreeDigits.ToCharArray()[1].ToString()) == 0)
                        {
                            if (int.Parse(ThreeDigits.ToCharArray()[2].ToString()) == 0)
                            {
                                return "مائتين";
                            }
                            return "مائتين" + " و " + convertOneDigits(ThreeDigits.ToCharArray()[2].ToString());
                        }
                        else
                        {
                            return "مائتين" + " و " + convertTwoDigits(ThreeDigits.Substring(1, 2));
                        }
                    }
                case 3:
                    {
                        if (int.Parse(ThreeDigits.ToCharArray()[1].ToString()) == 0)
                        {
                            if (int.Parse(ThreeDigits.ToCharArray()[2].ToString()) == 0)
                            {
                                return convertOneDigits(ThreeDigits.ToCharArray()[0].ToString()).Split('ه')[0] + "مائه";
                            }
                            return convertOneDigits(ThreeDigits.ToCharArray()[0].ToString()).Split('ه')[0] + "مائه" + " و " + convertOneDigits(ThreeDigits.ToCharArray()[2].ToString());
                        }
                        else
                        {
                            return convertOneDigits(ThreeDigits.ToCharArray()[0].ToString()).Split('ه')[0] + "مائه" + " و " + convertTwoDigits(ThreeDigits.Substring(1, 2));
                        }
                    }
                case 4:
                    {
                        goto case 3;
                    }
                case 5:
                    {
                        goto case 3;
                    }
                case 6:
                    {
                        goto case 3;
                    }
                case 7:
                    {
                        goto case 3;
                    }
                case 8:
                    {
                        goto case 3;
                    }
                case 9:
                    {
                        goto case 3;
                    }
                case 0:
                    {
                        if (ThreeDigits.ToCharArray()[1] == '0')
                        {
                            if (ThreeDigits.ToCharArray()[2] == '0')
                            {
                                return "";
                            }
                            else
                            {
                                return convertOneDigits(ThreeDigits.ToCharArray()[2].ToString());
                            }
                        }
                        else
                        {
                            return convertTwoDigits(ThreeDigits.Substring(1, 2));
                        }
                    }
                default: return "";
            }
        }
        private string convertFourDigits(string FourDigits)
        {
            switch (int.Parse(FourDigits.ToCharArray()[0].ToString()))
            {
                case 1:
                    {
                        if (int.Parse(FourDigits.ToCharArray()[1].ToString()) == 0)
                        {
                            if (int.Parse(FourDigits.ToCharArray()[2].ToString()) == 0)
                            {
                                if (int.Parse(FourDigits.ToCharArray()[3].ToString()) == 0)
                                    return "ألف";
                                else
                                {
                                    return "ألف" + " و " + convertOneDigits(FourDigits.ToCharArray()[3].ToString());
                                }
                            }
                            return "ألف" + " و " + convertTwoDigits(FourDigits.Substring(2, 2));
                        }
                        else
                        {
                            return "ألف" + " و " + convertThreeDigits(FourDigits.Substring(1, 3));
                        }
                    }
                case 2:
                    {
                        if (int.Parse(FourDigits.ToCharArray()[1].ToString()) == 0)
                        {
                            if (int.Parse(FourDigits.ToCharArray()[2].ToString()) == 0)
                            {
                                if (int.Parse(FourDigits.ToCharArray()[3].ToString()) == 0)
                                    return "ألفين";
                                else
                                {
                                    return "ألفين" + " و " + convertOneDigits(FourDigits.ToCharArray()[3].ToString());
                                }
                            }
                            return "ألفين" + " و " + convertTwoDigits(FourDigits.Substring(2, 2));
                        }
                        else
                        {
                            return "ألفين" + " و " + convertThreeDigits(FourDigits.Substring(1, 3));
                        }
                    }
                case 3:
                    {
                        if (int.Parse(FourDigits.ToCharArray()[1].ToString()) == 0)
                        {
                            if (int.Parse(FourDigits.ToCharArray()[2].ToString()) == 0)
                            {
                                if (int.Parse(FourDigits.ToCharArray()[3].ToString()) == 0)
                                    return convertOneDigits(FourDigits.ToCharArray()[0].ToString()) + " ألاف";
                                else
                                {
                                    return convertOneDigits(FourDigits.ToCharArray()[0].ToString()) + " ألاف" + " و " + convertOneDigits(FourDigits.ToCharArray()[3].ToString());
                                }
                            }
                            return convertOneDigits(FourDigits.ToCharArray()[0].ToString()) + " ألاف" + " و " + convertTwoDigits(FourDigits.Substring(2, 2));
                        }
                        else
                        {
                            return convertOneDigits(FourDigits.ToCharArray()[0].ToString()) + " ألاف" + " و " + convertThreeDigits(FourDigits.Substring(1, 3));
                        }
                    }
                case 4:
                    {
                        goto case 3;
                    }
                case 5:
                    {
                        goto case 3;
                    }
                case 6:
                    {
                        goto case 3;
                    }
                case 7:
                    {
                        goto case 3;
                    }
                case 8:
                    {
                        goto case 3;
                    }
                case 9:
                    {
                        goto case 3;
                    }
                default: return "";
            }
        }
        private string convertFiveDigits(string FiveDigits)
        {
            if (convertThreeDigits(FiveDigits.Substring(2, 3)).Length == 0)
            {
                return convertTwoDigits(FiveDigits.Substring(0, 2)) + " ألف ";
            }
            else
            {
                return convertTwoDigits(FiveDigits.Substring(0, 2)) + " ألفا " + " و " + convertThreeDigits(FiveDigits.Substring(2, 3));
            }
        }
        private string convertSixDigits(string SixDigits)
        {
            if (convertThreeDigits(SixDigits.Substring(2, 3)).Length == 0)
            {
                return convertThreeDigits(SixDigits.Substring(0, 3)) + " ألف ";
            }
            else
            {
                return convertThreeDigits(SixDigits.Substring(0, 3)) + " ألفا " + " و " + convertThreeDigits(SixDigits.Substring(3, 3));
            }
        }

        public string GetNumberAr()
        {

            return NumberAr;
        }


    }
 

Points of Interest

I think it is a good practise using switch and the divide and conqure technique.

History

  • 07 November 2012: Initial version.
  • 08 November 2012 : Updated
  • 11 November 2012 : Updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberibrahim_ragab20-May-13 15:09 
GeneralMy vote of 5 PinmemberAhmed Ibrahim Assaf10-Dec-12 22:31 
Questionbug Pinmembersastocker9-Nov-12 5:15 
AnswerRe: bug Pinmemberahmed_alashrafy11-Nov-12 3:39 
GeneralMy vote of 1 Pinmembergopal pradhan8-Nov-12 20:45 
poor
GeneralMy vote of 3 Pinmembermoha77-Nov-12 23:12 
GeneralRe: My vote of 3 Pinmemberahmed_alashrafy8-Nov-12 0:33 
GeneralRe: My vote of 3 Pinmemberahmed_alashrafy8-Nov-12 0:35 
GeneralRe: My vote of 3 Pinmemberahmed_alashrafy8-Nov-12 7:14 
GeneralRe: My vote of 3 Pinmemberahmed_alashrafy8-Nov-12 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 12 Nov 2012
Article Copyright 2012 by ahmed_alashrafy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid