Created
July 24, 2021 09:52
-
-
Save sangkukbae12/bdb9d19cc3102534bd6344f507e0bf35 to your computer and use it in GitHub Desktop.
test2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
const data = [ | |
['오백삼십조칠천팔백구십만천오백삼십구', '삼조사천이만삼천구'], | |
['육십사억삼천십팔만칠천육백구', '사십삼'], | |
['구백육십조칠천억팔천백삼십이만칠천일', '사십삼조오천이백억육천구백십만일'], | |
['이천구백육십조천오백칠십만삼천구백구십', '삼천사백오십조일억이천만육백사십삼'], | |
['사십오억삼천육십만오백구십', '칠십억천이백삼십오만칠천구십이'], | |
['천백십일', '구천오백구십구'], | |
['오억사천', '백십일'], | |
['만오천사백삼십', '십구만삼천오백'], | |
['일조', '삼'], | |
['일억', '만'] | |
]; | |
data.forEach(item => sumKorNum(item)); | |
} | |
test(); | |
function sumKorNum(arr) { | |
const sum = str2Num(arr[0]) + str2Num(arr[1]); | |
const convertedSum = num2Str(sum); | |
console.log(convertedSum); | |
} | |
function str2Num(str) { | |
const numMap = new Map([ | |
[ '일', 1 ], | |
[ '이', 2 ], | |
[ '삼', 3 ], | |
[ '사', 4 ], | |
[ '오', 5 ], | |
[ '육', 6 ], | |
[ '칠', 7 ], | |
[ '팔', 8 ], | |
[ '구', 9 ] | |
]) | |
const unitMap = new Map([ | |
[ '십', 10 ], | |
[ '백', 100 ], | |
[ '천', 1000 ], | |
]) | |
const multiUnitMap = new Map([ | |
[ '만', 10000 ], | |
[ '억', 100000000 ], | |
[ '조', 1000000000000 ], | |
[ '경', 10000000000000000 ] | |
]) | |
const arr = []; | |
let n = 1, unit = 0, sum = 0; | |
for (let i = 0; i < str.length; i++) { | |
let c = str[i]; | |
if (numMap.has(c)) { | |
n = numMap.get(c) | |
} else if (unitMap.has(c)) { | |
unit = unitMap.get(c) | |
sum += n ? n * unit : unit; | |
n = 0; | |
} else if (multiUnitMap.has(c)) { | |
sum = n | |
? (sum + n) * multiUnitMap.get(c) | |
: sum * multiUnitMap.get(c) | |
arr.push(sum); | |
sum = 0; | |
n = 0; | |
} | |
} | |
if (sum) { | |
sum = n ? sum + n : sum; | |
arr.push(sum); | |
} else { | |
if (n) { | |
arr.push(n); | |
} | |
} | |
const rst = arr.reduce((acc, cur) => acc + cur, 0); | |
return rst; | |
} | |
function num2Str(num) { | |
const numMap = new Map([ | |
[ "1", '일' ], | |
[ "2", '이' ], | |
[ "3", '삼' ], | |
[ "4", '사' ], | |
[ "5", '오' ], | |
[ "6", '육' ], | |
[ "7", '칠' ], | |
[ "8", '팔' ], | |
[ "9", '구' ] | |
]) | |
const units = ['', '십', '백', '천']; | |
const multiUnits = ['', '만', '억', '조', '경']; | |
const arr = []; | |
while (num > 0) { | |
const str = (num % 10000).toString().split(''); | |
const _str = str.reverse().reduce((acc, cur, idx) => { | |
if (cur === '0') { | |
return acc | |
} else { | |
return (cur === '1' && idx > 0) | |
? units[idx] + acc | |
: numMap.get(cur) + units[idx] + acc | |
} | |
}, '') | |
arr.push(_str) | |
num = parseInt(num / 10000) | |
} | |
const rst = arr.reduce((acc, cur, idx) => { | |
return cur | |
? cur + multiUnits[idx] + acc | |
: acc | |
}, '') | |
return rst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment