Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save dohyunkim/f4072c652fef3d1ba878 to your computer and use it in GitHub Desktop.

Select an option

Save dohyunkim/f4072c652fef3d1ba878 to your computer and use it in GitHub Desktop.
한자 갖은자로 숫자를 표시한다.
\edef\legalhanjanumATcatcode{\catcode`@=\the\catcode`@\relax}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname ProvidesPackage\endcsname\relax\else
\ProvidesPackage{legalhanjanum}[2014/06/27 v0.1 number to hanja number]
\fi
\catcode`@=11
\protected\def\legalhanjanum#1{%
\begingroup\edef\x{#1}\count@\z@\toks@{}\expandafter\hanjanum@\x\@@\endgroup
}
\def\hanjanum@#1{% 토큰 순서 뒤집기
\ifx#1\@@
\def\next{\expandafter\hanjanum@@\the\toks@\empty\empty\empty\empty\@@}%
\else
\ifx#1,\else % comma 무시
\ifnum#1>\z@\count@\@ne\fi
\toks@\expandafter{\expandafter#1\the\toks@}%
\fi
\let\next\hanjanum@
\fi
\next
}
\def\hanjanum@@#1\@@{%
\ifnum\count@=\z@ % 모든 숫자가 0 or empty arg
\numtohanjaXX\z@
\else
\count@\z@\toks@{}\hanjanum@@@#1\@@
\fi
}
\def\hanjanum@@@#1#2#3#4#5\@@{%
\ifnum#1#2#3#4>\z@ % 만 단위
\ifnum\count@>\z@
\edef\x{\toks@{\numtohanjaXX\count@\the\toks@}}\x
\fi
\fi
\def\next{\the\toks@}% 기본값: 출력
\ifx\empty#1\else \hanjanum@@@@#1\z@ % 일 단위
\ifx\empty#2\else \hanjanum@@@@#2\@ne % 십 단위
\ifx\empty#3\else \hanjanum@@@@#3\tw@ % 백 단위
\ifx\empty#4\else \hanjanum@@@@#4\thr@@ % 천 단위
\ifx\empty#5\else
\def\next{\hanjanum@@@#5\@@}% 남은 숫자 재귀
\fi
\fi
\fi
\fi
\fi
\advance\count@\@ne % 만 단위 카운터
\next
}
\def\hanjanum@@@@#1#2{%
\ifnum#1>\z@
\edef\x{\toks@{\numtohanja#1\ifnum#2>\z@\numtohanjaX#2\fi\the\toks@}}\x
\fi
}
\def\@legalhanjanum#1{\legalhanjanum{\the#1}}
\legalhanjanumATcatcode
\def\numtohanja#1{%
\ifcase#1\or 壹\or 貳\or 參\or 四\or 伍\or 六\or 七\or 八\or 九\fi
}
\def\numtohanjaX#1{%
\ifcase#1\or 拾\or 百\or 阡\fi
}
\def\numtohanjaXX#1{%
\ifcase#1零\or 萬\or 億\or 兆\or 京\or 垓\or 秭\or 壤\or 溝\or 澗\or
正\or 載\or 極\else\errmessage{Number too big}\fi
}
\endinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment