Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 소수점 추가 후 발생한 '영'읽기 버그 수정 #159

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-cups-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

fix: 소수점 추가 후 발생한 '영'읽기 버그 수정
5 changes: 5 additions & 0 deletions src/amountToHangul.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ describe('amountToHangul', () => {
expect(amountToHangul('15,201,100')).toEqual('일천오백이십만천백');
expect(amountToHangul('100000000')).toEqual('일억');
expect(amountToHangul('100000100')).toEqual('일억백');
expect(amountToHangul('0')).toEqual('영');
expect(amountToHangul('')).toEqual('');
});

it('숫자 외 문자를 무시하여 반환', () => {
expect(amountToHangul('120,030원')).toEqual('일십이만삼십');
});

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('일만이천삼백사십오점육칠팔구');
});
Expand Down
40 changes: 23 additions & 17 deletions src/amountToHangul.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])]);
}
}

Expand Down