Skip to content

Instantly share code, notes, and snippets.

@sonbyungjun
Created June 2, 2020 02:40
Show Gist options
  • Save sonbyungjun/c1116ad4df0cb3a41db2c43313b74217 to your computer and use it in GitHub Desktop.
Save sonbyungjun/c1116ad4df0cb3a41db2c43313b74217 to your computer and use it in GitHub Desktop.
Typescript(Javascript) conversion of amount into Korean Function (타입스크립트(자바스크립트) 금액 한글로 변환 함수)
export const viewKorean = (num: any): string => {
num = parseInt((num + '').replace(/[^0-9]/g, ''), 10) + '';
if (num == '0') return '영';
let number = ['영', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구'];
let unit = ['', '만', '억', '조'];
let smallUnit = ['천', '백', '십', ''];
let result = [];
let unitCnt = Math.ceil(num.length / 4);
num = num.padStart(unitCnt * 4, '0');
let regexp = /[\w\W]{4}/g;
let array = num.match(regexp);
for (let i = array.length - 1, unitCnt = 0; i >= 0; i--, unitCnt++) {
let hanValue = _makeHan(array[i]);
if (hanValue == '') continue;
result.unshift(hanValue + unit[unitCnt]);
}
function _makeHan(text: any) {
let str = '';
for (let i = 0; i < text.length; i++) {
let num = text[i];
if (num == '0')
//0은 읽지 않는다
continue;
str += number[num] + smallUnit[i];
}
return str;
}
return result.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment