J2EE List


تبدیل عدد به حروف در جاوا

محسن صالحی -

 

 public static string NumberToText(string Input)
        {
            StringBuilder output = new StringBuilder();
            if (Input != “”)
            {
                string temp = “”;
                int i = Input.Length – 1;//because index start with 0
                int j = 0;
                int part = 0;
                while (i >= 0)
                {
                    temp = Convert.ToString(Input[i]);
                    if ((i != 0) && (j == 0) & (Input[i - 1] == ’1′))//( 0xf8 & 0x3f); bitwise and —>0×38
                    {
                        temp = Input.Substring(i – 1, 2);
                        temp = TwoDigitToText(temp);
                        output.Insert(0, temp);
                        if ((i != 0) & ((i – 1) != 0))
                            output.Insert(0, ” و “);
                        i = i – 2;
                        j++;
                    }
                    else
                    {
                        temp = DigitToText(temp, j);
                        if (temp != “”)
                        {
                            output.Insert(0, temp);
                            if (i != 0)
                                output.Insert(0, ” و “);
                        }
                        i–;
                    }
                    if (j == 2)
                    {
                        part++;
                        if (i != -1)
                            output.Insert(0, ” ” + PartsName(part) + ” “);
                        j = 0;
                    }
                    else
                        j++;
                }
            }
            return output.ToString();
        }
        public static string PartsName(int part)
        {
            string output = “”;
            switch (part)
            {
                case 0:
                    output = “”;
                    break;
                case 1:
                    output = “هزار”;
                    break;
                case 2:
                    output = “میلون”;
                    break;
                case 3:
                    output = “میلیارد”;
                    break;
                default:
                    break;
            }
            return output;
        }
        public static string DigitToText(string Digit, int Order)
        {
            string output = “”;
            if (Order == 0)
            {
                switch (Digit)
                {
                    case “0″:
                        output = “”;
                        break;
                    case “1″:
                        output = “یک”;
                        break;
                    case “2″:
                        output = “دو”;
                        break;
                    case “3″:
                        output = “سه”;
                        break;
                    case “4″:
                        output = “چهار”;
                        break;
                    case “5″:
                        output = “پنج”;
                        break;
                    case “6″:
                        output = “شش”;
                        break;
                    case “7″:
                        output = “هفت”;
                        break;
                    case “8″:
                        output = “هشت”;
                        break;
                    case “9″:
                        output = “نه”;
                        break;
                    default:
                        output = “”;
                        break;
                }
            }
            if (Order == 1)
            {
                switch (Digit)
                {
                    case “0″:
                        output = “”;
                        break;
                    case “1″:
                        output = “”;
                        break;
                    case “2″:
                        output = “بیست”;
                        break;
                    case “3″:
                        output = “سی”;
                        break;
                    case “4″:
                        output = “چهل”;
                        break;
                    case “5″:
                        output = “پنجاه”;
                        break;
                    case “6″:
                        output = “شصت”;
                        break;
                    case “7″:
                        output = “هفتاد”;
                        break;
                    case “8″:
                        output = “هشتاد”;
                        break;
                    case “9″:
                        output = “نود”;
                        break;
                    default:
                        output = “”;
                        break;
                }
            }
            if (Order == 2)
            {
                switch (Digit)
                {
                    case “0″:
                        output = “”;
                        break;
                    case “1″:
                        output = “صد”;
                        break;
                    case “2″:
                        output = “دویست”;
                        break;
                    case “3″:
                        output = “سیصد”;
                        break;
                    case “4″:
                        output = “چهارصد”;
                        break;
                    case “5″:
                        output = “پانصد”;
                        break;
                    case “6″:
                        output = “ششصد”;
                        break;
                    case “7″:
                        output = “هفتصد”;
                        break;
                    case “8″:
                        output = “هشتصد”;
                        break;
                    case “9″:
                        output = “نهصد”;
                        break;
                    default:
                        output = “”;
                        break;
                }
            }
            return output;
        }
        public static string TwoDigitToText(string Digits)
        {
            string output = “”;
            switch (Digits)
            {
                case “10″:
                    output = “ده”;
                    break;
                case “11″:
                    output = “یازده”;
                    break;
                case “12″:
                    output = “دوازده”;
                    break;
                case “13″:
                    output = “سیزده”;
                    break;
                case “14″:
                    output = “چهارده”;
                    break;
                case “15″:
                    output = “پانزده”;
                    break;
                case “16″:
                    output = “شانزده”;
                    break;
                case “17″:
                    output = “هفده”;
                    break;
                case “18″:
                    output = “هجده”;
                    break;
                case “19″:
                    output = “نوزده”;
                    break;
                default:
                    output = “”;
                    break;
            }
            return output;
        }

 

  • رجبی

    دم آقا محسن گرم