본문 바로가기
개발언어/C#

[C#]숫자를 한글로 표시하기 (C# 숫자 한글 변환)

by 창용이랑 2021. 11. 25.
728x90

 

전체소스코드

using System;
using System.Linq;
using System.Windows.Forms;

namespace NumberToKorean
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //화면 실행 버튼 클릭
        private void button_Run_Click(object sender, EventArgs e)
        {
            long amount;
            string amountString;
            string wordUnit;
            string amountWord = "";
            int amountCount;

            string unitM = "";
            string unitE = "";
            string unitJ = "";
            string unitK = "";

            //textBox_Before.Text : Form의 TextBox (숫자로 입력)
            amount = Convert.ToInt64(textBox_Before.Text);
            amountString = new String(amount.ToString().ToCharArray().Reverse().ToArray());
            amountCount = amountString.Length;

            for (int i = 1; i <= amountCount; i++)
            {
                wordUnit = AmountToWord(Convert.ToInt32(amountString.Substring(i - 1, 1)), i);

                if (unitM.Equals("") && wordUnit != "" && (i >= 5 && i <= 8))
                {
                    unitM = "만";
                    amountWord = wordUnit + unitM + amountWord;
                }
                else if (unitE.Equals("") && wordUnit != "" && (i >= 9 && i <= 12))
                {
                    unitE = "억";
                    amountWord = wordUnit + unitE + amountWord;
                }
                else if (unitJ.Equals("") && wordUnit != "" && (i >= 13 && i <= 16))
                {
                    unitJ = "조";
                    amountWord = wordUnit + unitJ + amountWord;
                }
                else if (unitK.Equals("") && wordUnit != "" && (i >= 17 && i <= 20))
                {
                    unitK = "경";
                    amountWord = wordUnit + unitK + amountWord;
                }
                else
                {
                    amountWord = wordUnit + amountWord;
                }
            }

            //textBox_After.Text : Form의 TextBox (textBox_Before.Text의 숫자로 입력된 값을 한글로 입력)
            textBox_After.Text = amountWord;
        }

        static string AmountToWord(int amountEach, int count)
        {
            string word = "";

            switch (amountEach)
            {
                case 1:
                    word = "일";
                    break;
                case 2:
                    word = "이";
                    break;
                case 3:
                    word = "삼";
                    break;
                case 4:
                    word = "사";
                    break;
                case 5:
                    word = "오";
                    break;
                case 6:
                    word = "육";
                    break;
                case 7:
                    word = "칠";
                    break;
                case 8:
                    word = "팔";
                    break;
                case 9:
                    word = "구";
                    break;
                case 10:
                    word = "십";
                    break;
            }
            if(amountEach != 0)
            {
                switch (count)
                {
                    case 1:
                        word += "";
                        break;
                    case 2:
                        word += "십";
                        break;
                    case 3:
                        word += "백";
                        break;
                    case 4:
                        word += "천";
                        break;
                    case 5:
                        word += "";
                        break;
                    case 6:
                        word += "십";
                        break;
                    case 7:
                        word += "백";
                        break;
                    case 8:
                        word += "천";
                        break;
                    case 9:
                        word += "";
                        break;
                    case 10:
                        word += "십";
                        break;
                    case 11:
                        word += "백";
                        break;
                    case 12:
                        word += "천";
                        break;
                    case 13:
                        word += "";
                        break;
                    case 14:
                        word += "십";
                        break;
                    case 15:
                        word += "백";
                        break;
                    case 16:
                        word += "천";
                        break;
                    case 17:
                        word += "";
                        break;
                    case 18:
                        word += "십";
                        break;
                    case 19:
                        word += "백";
                        break;
                    case 20:
                        word += "천";
                        break;
                }
            }
         
            return word;
        }
    }
}

 

출처 : https://andy0930.blogspot.com/2018/08/c-c.html