Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created March 5, 2018 14:19
Show Gist options
  • Save zaltoprofen/65cc37c825a3aad8b79e5282c747249e to your computer and use it in GitHub Desktop.
Save zaltoprofen/65cc37c825a3aad8b79e5282c747249e to your computer and use it in GitHub Desktop.
function kanjinize(v) {
const minor_suffix = ['', '十', '百', '千'];
const major_suffix = ['', '万', '億', '兆', '京'];
const digit = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
if (typeof v != 'number' || v % 1 != 0 || v < 0) {
return;
}
if (v == 0) {
return '零';
}
let s = '';
let idx = 0;
for (; v > 0; v = Math.floor(v/10), idx++) {
let ss = '';
if (v % 10 != 0) {
ss = digit[v % 10] + minor_suffix[idx % 4];
}
if (idx % 4 == 0) {
if (major_suffix.indexOf(s[0]) != -1) s = s.substr(1);
ss = ss + major_suffix[idx/4];
}
s = ss + s;
}
if (s[0] == '一' && s.length > 1) s = s.substr(1);
return s;
}
function m尾n合子(m, n) {
return kanjinize(m) + '尾' + kanjinize(n) + '合子';
}
function randInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
const MAX = Number.MAX_SAFE_INTEGER;
console.log(m尾n合子(7, 100));
console.log(m尾n合子(randInt(0, 101), randInt(0, 101)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment