diff --git a/.changeset/big-cups-call.md b/.changeset/big-cups-call.md new file mode 100644 index 00000000..9b055d10 --- /dev/null +++ b/.changeset/big-cups-call.md @@ -0,0 +1,5 @@ +--- +"es-hangul": patch +--- + +fix: 소수점 추가 후 발생한 '영'읽기 버그 수정 diff --git a/src/amountToHangul.spec.ts b/src/amountToHangul.spec.ts index 0ba591a6..f7c47c24 100644 --- a/src/amountToHangul.spec.ts +++ b/src/amountToHangul.spec.ts @@ -5,6 +5,8 @@ describe('amountToHangul', () => { expect(amountToHangul('15,201,100')).toEqual('일천오백이십만천백'); expect(amountToHangul('100000000')).toEqual('일억'); expect(amountToHangul('100000100')).toEqual('일억백'); + expect(amountToHangul('0')).toEqual('영'); + expect(amountToHangul('')).toEqual(''); }); it('숫자로 된 금액이 80글자를 넘을 시 에러 발생', () => { @@ -17,6 +19,9 @@ describe('amountToHangul', () => { }); it('소수점이 있는 경우도 표기', () => { + expect(amountToHangul('0.01020')).toEqual('영점영일영이'); + expect(amountToHangul('0.0000')).toEqual('영'); + expect(amountToHangul('.0000')).toEqual('영'); expect(amountToHangul('392.24')).toEqual('삼백구십이점이사'); expect(amountToHangul('12345.6789')).toEqual('일만이천삼백사십오점육칠팔구'); }); diff --git a/src/amountToHangul.ts b/src/amountToHangul.ts index c0afbd53..3d70f9d5 100644 --- a/src/amountToHangul.ts +++ b/src/amountToHangul.ts @@ -22,48 +22,54 @@ export const HANGUL_DIGITS = [ ]; export const HANGUL_DIGITS_MAX = HANGUL_DIGITS.length * 4; export const HANGUL_NUMBERS = ['', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구']; +export const HANGUL_NUMBERS_FOR_DECIMAL = ['영', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구']; export const HANGUL_CARDINAL = ['', '십', '백', '천']; export function amountToHangul(amount: string | number) { - const [integerPart, decimalPart] = String(amount) + const [integerPart, tempDecimalPart] = String(amount) .replace(/[^\d.]+/g, '') .split('.'); if (integerPart.length > HANGUL_DIGITS_MAX) { throw new Error(`convert range exceeded : ${amount}`); } + const decimalPart = tempDecimalPart?.replace(/0+$/, ''); const result = []; let pronunDigits = true; - for (let i = 0; i < integerPart.length - 1; i++) { - const digit = integerPart.length - i - 1; + if(integerPart === '0' || (integerPart === '' && tempDecimalPart)) { + result.push(HANGUL_NUMBERS_FOR_DECIMAL[0]); + } else { + for (let i = 0; i < integerPart.length - 1; i++) { + const digit = integerPart.length - i - 1; - if (integerPart[i] > '1' || digit % 4 === 0 || i === 0) { - const hangulNumber = HANGUL_NUMBERS[Number(integerPart[i])]; + if (integerPart[i] > '1' || digit % 4 === 0 || i === 0) { + const hangulNumber = HANGUL_NUMBERS[Number(integerPart[i])]; - if (hangulNumber) { - result.push(hangulNumber); - pronunDigits = true; + if (hangulNumber) { + result.push(hangulNumber); + pronunDigits = true; + } } - } - if (pronunDigits && digit % 4 === 0) { - result.push(HANGUL_DIGITS[digit / 4]); - pronunDigits = false; - } + if (pronunDigits && digit % 4 === 0) { + result.push(HANGUL_DIGITS[digit / 4]); + pronunDigits = false; + } - if (integerPart[i] !== '0') { - result.push(HANGUL_CARDINAL[digit % 4]); + if (integerPart[i] !== '0') { + result.push(HANGUL_CARDINAL[digit % 4]); + } } + result.push(HANGUL_NUMBERS[Number(integerPart[integerPart.length - 1])]); } - result.push(HANGUL_NUMBERS[Number(integerPart[integerPart.length - 1])]); if (decimalPart) { result.push('점'); for (let i = 0; i < decimalPart.length; i++) { - result.push(HANGUL_NUMBERS[Number(decimalPart[i])]); + result.push(HANGUL_NUMBERS_FOR_DECIMAL[Number(decimalPart[i])]); } }