Skip to content

Instantly share code, notes, and snippets.

@DillonMemo
Last active January 24, 2021 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DillonMemo/6912ec7f1709c779b01134ec856dc098 to your computer and use it in GitHub Desktop.
Save DillonMemo/6912ec7f1709c779b01134ec856dc098 to your computer and use it in GitHub Desktop.
miso
/**
* Test Data
* const DATA = [
* ['오백삼십조칠천팔백구십만천오백삼십구', '삼조사천이만삼천구'],
* ['육십사억삼천십팔만칠천육백구', '사십삼'],
* ['구백육십조칠천억팔천백삼십이만칠천일', '사십삼조오천이백억육천구백십만일'],
* ['이천구백육십조천오백칠십만삼천구백구십', '삼천사백오십조일억이천만육백사십삼'],
* ['사십오억삼천육십만오백구십', '칠십억천이백삼십오만칠천구십이'],
* ['천백십일', '구천오백구십구'],
* ['오억사천', '백십일'],
* ['만오천사백삼십', '십구만삼천오백'],
* ['일조', '삼'],
* ['일억', '만'],
* ];
*/
const KrToNum = (concat) => {
let sumResult = 0;
for (let i = 0; i < concat.length; i++) {
const numUnitSymbol = ['', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구'];
const dotUnitSymbol = ['', '만', '억', '조'];
const unitSymbol = { 십: 10, 백: 100, 천: 1000, 만: 10000, 억: 100000000, 조: 1000000000000 };
let result = (tmpResult = num = 0);
for (let j = 0; j < concat[i].length; j++) {
const token = concat[i][j];
const check = numUnitSymbol.indexOf(concat[i][j]);
// 만, 억, 조, 천, 백, 십
if (check === -1) {
if (dotUnitSymbol.indexOf(token) === -1) {
// 십 백 천
tmpResult += (num !== 0 ? num : 1) * unitSymbol[token];
} else {
// 만, 억, 조
tmpResult += num;
result += (tmpResult !== 0 ? tmpResult : 1) * unitSymbol[token];
tmpResult = 0;
}
num = 0;
} else {
num = check;
}
}
sumResult += result + tmpResult + num;
}
return sumResult;
};
const numToKr = (num) => {
const numUnitSymbol = ['', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구'];
const unitSymbol = ['', '십', '백', '천'];
const dotUnitSymbol = ['', '만', '억', '조'];
const result = num
.toString()
.split('')
.map((num) => +num)
.reverse()
.map((num, idx) => {
const remainderIndex = idx % 4;
const dotIndex = Math.ceil(idx / 4);
const text = numUnitSymbol[num];
const remainder = num === 0 ? '' : unitSymbol[remainderIndex];
const dot = remainderIndex === 0 ? dotUnitSymbol[dotIndex] : '';
const bool = text === '일' && unitSymbol.indexOf(remainder);
// console.log({ idx, num, text, remainderIndex, remainder, dotIndex, dot, bool });
return bool ? `${remainder}${dot}` : `${text}${remainder}${dot}`;
});
const resultArr = [];
while (result.length > 0) {
resultArr.push(result.splice(0, 4));
}
return resultArr
.filter((str) => {
// console.log(!dotUnitSymbol.includes(str.join('')), str.join(''));
return !dotUnitSymbol.includes(str.join(''));
})
.flat()
.reverse()
.join('');
// return result.reverse().join('');
};
const DATA = [
['오백삼십조칠천팔백구십만천오백삼십구', '삼조사천이만삼천구'],
['육십사억삼천십팔만칠천육백구', '사십삼'],
['구백육십조칠천억팔천백삼십이만칠천일', '사십삼조오천이백억육천구백십만일'],
['이천구백육십조천오백칠십만삼천구백구십', '삼천사백오십조일억이천만육백사십삼'],
['사십오억삼천육십만오백구십', '칠십억천이백삼십오만칠천구십이'],
['천백십일', '구천오백구십구'],
['오억사천', '백십일'],
['만오천사백삼십', '십구만삼천오백'],
['일조', '삼'],
['일억', '만'],
];
for (let i = 0; i < DATA.length; i++) {
console.log(numToKr(KrToNum(DATA[i])));
}
// console.log(numToKr(KrToNum(DATA[0])));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment