■ 한글 숫자 문자열을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
using System.Text; #region 한글 숫자 문자열 구하기 - GetHangulNumberString(source) /// <summary> /// 한글 숫자 문자열 구하기 /// </summary> /// <param name="source">소스 값</param> /// <returns>한글 숫자 문자열</returns> public static string GetHangulNumberString(int source) { if(source == 0) { return "영"; } StringBuilder stringBuilder = new StringBuilder(); string integerString = source.ToString().PadLeft(16, ' '); for(int i = 0; i < integerString.Length; i++) { switch(integerString[i]) { case '0' : stringBuilder.Append(" "); break; case '1' : stringBuilder.Append("일"); break; case '2' : stringBuilder.Append("이"); break; case '3' : stringBuilder.Append("삼"); break; case '4' : stringBuilder.Append("사"); break; case '5' : stringBuilder.Append("오"); break; case '6' : stringBuilder.Append("육"); break; case '7' : stringBuilder.Append("칠"); break; case '8' : stringBuilder.Append("팔"); break; case '9' : stringBuilder.Append("구"); break; default : stringBuilder.Append(" "); break; } } string unitJO = string.Empty; string unitUK = string.Empty; string unitMAN = string.Empty; string unitWON = string.Empty; unitJO = GetFormatUnitString(stringBuilder.ToString(stringBuilder.Length - 16, 4)); unitUK = GetFormatUnitString(stringBuilder.ToString(stringBuilder.Length - 12, 4)); unitMAN = GetFormatUnitString(stringBuilder.ToString(stringBuilder.Length - 8 , 4)); unitWON = GetFormatUnitString(stringBuilder.ToString(stringBuilder.Length - 4 , 4)); if(!string.IsNullOrEmpty(unitMAN)) { unitMAN += "만"; } if(!string.IsNullOrEmpty(unitUK)) { unitUK += "억"; } if(!string.IsNullOrEmpty(unitJO)) { unitJO += "조"; } return unitJO + unitUK + unitMAN + unitWON; } #endregion #region 형식 단위 문자열 구하기 - GetFormatUnitString(source) /// <summary> /// 형식 단위 문자열 구하기 /// </summary> /// <param name="source">소스 문자열</param> /// <returns>형식 단위 문자열</returns> private string GetFormatUnitString(string source) { string one = string.Empty; string ten = string.Empty; string hundred = string.Empty; string thousand = string.Empty; if(source[0] != ' ') { thousand = source[0].ToString() + "천"; } if(source[1] != ' ') { hundred = source[1].ToString() + "백"; } if(source[2] != ' ') { ten = source[2].ToString() + "십"; } if(source[3] != ' ') { one = source[3].ToString(); } return string.Format("{0}{1}{2}{3}", thousand, hundred, ten, one); } #endregion |