Skip to content

Instantly share code, notes, and snippets.

@rsato rsato/file0.groovy
Created May 15, 2014

Embed
What would you like to do?
「一反田えー(1反田A)」から「千反田える」までを生成する ref: http://qiita.com/rsato/items/a0013f35f731ff6b494f
def alphabetList = ["えー", "びー", "しー", "でぃー", "いー", "えふ", "じー", "えいち", "あい", "じぇい", "けー", "える", "えむ", "えぬ", "おー", "ぴー", "きゅー", "あーる", "えす", "てぃー", "ゆー", "ぶい", "だぶりゅー", "えっくす", "わい", "ぜっと"]
def range = 1..1000
range.eachWithIndex {num, i->
println "${arabicNumToKanjiNum(num)}反田${alphabetList[i%alphabetList.size()]}"
}
// アラビア数字を漢数字に変換します
def arabicNumToKanjiNum(long num){
def kanjiNumUnit = ["", "", "", "", ""]
// 4桁ごとのブロックの数
int blockIndex = num.toString().size() / 4
def kanji = ""
while (blockIndex >= 0) {
// ブロックの値のみを残す
int value = num / (10000 ** blockIndex)
int blockNum = value % 10000
// ブロックの値が1以上のとき
if (blockNum >= 1) {
kanji = "${kanji}${arabicNumToKanjiNumSub(blockNum)}${kanjiNumUnit[blockIndex]}"
}
blockIndex--
}
kanji
}
// 一万未満のアラビア数字を漢数字に変換します
def arabicNumToKanjiNumSub(int num){
def kanjiNum = ["", "", "", "", "", "", "", "", "", "" ]
def kanjiNumSubUnit = ["", "", "", ""]
// 数字の桁数
int digitIndex = num.toString().size() - 1
def kanji = ""
while (digitIndex >= 0) {
// 指定した桁の値のみを残す
int value = num / (10 ** digitIndex)
int numIndex = value % 10
// 桁の数字が2以上(「一百」などを避ける)
// または1桁目の数字(1桁目は「一」を表示)の場合
if (numIndex >= 2 || digitIndex == 0) { //
// 漢数字を追加
kanji = "${kanji}${kanjiNum[numIndex]}"
}
// 桁の数字が1以上のとき
if(numIndex >= 1) {
// 百などを表示
kanji = "${kanji}${kanjiNumSubUnit[digitIndex]}"
}
digitIndex--
}
kanji
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.