C#/Console
[C#] 금액 (숫자) -> 한글 (원)로 변환하기
스타크래프트 좋아하는 사람
2023. 1. 23. 16:53
업무 중 필요해서 만들어 본 금액을 한글로 표시해주는 코드입니다.
만들 때 마다 코드가 달라지는데 기본적인 방식은 비슷하기에 저장..
using System;
namespace ConsoleApp2
{
class Program
{
private static readonly string[] num = { "", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" };
private static readonly string[] digit = { "", "십", "백", "천" };
private static readonly string[] word = { "", "만 ", "억 ", "조 ", "경 " }; //띄어쓰기 포함시켰습니다.
//숫자 -> 한글 값
public static void Main(string[] args)
{
string result = string.Empty;
string sign = string.Empty;
string readLine = Console.ReadLine();
int wordIndex = 0;
long input = 0;
if (readLine[0] == '-') //음수 양수 확인
{
sign = "-";
}
//숫자만 추출 (그냥 만들어본거 ㅋ)
for (int i = 0; i < readLine.Length; ++i)
{
if ('0' <= readLine[i] && readLine[i] <= '9')
{
input = input * 10 + readLine[i] - '0';
}
}
while (input > 0)
{
string subResult = "";
for (int i = 0; i < 4; ++i) // 1, 10, 100, 1000 단위별로 확인합니다.
{
long val = input % 10;
input /= 10;
if (val == 0) //0은 출력하지 않습니다.
{
continue;
}
if (val == 1 && i != 0) //일천, 일백, 일십에서는 일을 제거해줍니다.
{
subResult = digit[i] + subResult;
}
else //나머지 숫자들에 대해선 정상 출력해줍니다.
{
subResult = num[val] + digit[i] + subResult;
}
}
if (subResult != "") //값이 있다면 추가해줍니다.
{
result = subResult + word[wordIndex] + result;
}
wordIndex++;
}
if (string.IsNullOrEmpty(result))
{
Console.WriteLine("영원");
}
else
{
Console.WriteLine("{0}{1}원", sign, result);
}
}
}
}