- formatMoney(정수) · formatLineMoney(소수 1 · 끝 0 없음) · formatPrice(단가 소수 3 · 200 미만 유효숫자 넷) · formatNumber(수량 · 율 유효숫자 넷) - M01 Logic_Money · Logic_Edit 는 다시 내보내기만(부르는 쪽 그대로) - 파이썬 짝 master_fmt.py(절반 올림 · 은행가 반올림 어긋남 해소) · master_text 갈아 끼움은 받기 뒤 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
47 lines
2.2 KiB
TypeScript
47 lines
2.2 KiB
TypeScript
/* =============================================================================
|
|
* common_util_money.ts
|
|
* 화면 숫자 글 꼴 한 벌 — 금액 · 단가 · 수량 · 율 · 빈 값 · 음수. 셈 값은 그대로, 보이는 글만 만듦(반올림).
|
|
* formatMoney 원 단위 정수 (계 · 소계 · 합계 · 내역 금액)
|
|
* formatLineMoney 최대 소수 1 · 끝 0 뗌 (일위대가 줄 금액 · 산근 근거 줄)
|
|
* formatPrice 단가 — 200 원 미만 = 유효숫자 넷 · 그 밖 = 최대 소수 3 · 끝 0 뗌
|
|
* formatNumber 수량 · 율 · 품 — 소수 넷째 자리 · 유효숫자 넷 중 많은 쪽 · 끝 0 뗌
|
|
* 값이 숫자가 아니면 빈 값 = 「」 · 객체 = JSON 글 · 그 밖 = String.
|
|
* 짝: `resources/master_data/scripts/master_fmt.py` (거울 시험 `resources/tester/test_588_2_money_fmt.py`).
|
|
* ========================================================================== */
|
|
|
|
const LOCALE = "ko-KR";
|
|
|
|
function fixed(value: number, maxDigits: number): string {
|
|
const text = value.toLocaleString(LOCALE, { maximumFractionDigits: Math.min(20, maxDigits) });
|
|
return text === "-0" ? "0" : text;
|
|
}
|
|
|
|
/** 수량 꼴 자리 — 소수 넷째 자리 · 유효숫자 넷 중 많은 쪽 (0.00167 → 0.00167) */
|
|
function numberPlaces(value: number): number {
|
|
return value === 0 || !Number.isFinite(value)
|
|
? 4
|
|
: Math.max(4, 3 - Math.floor(Math.log10(Math.abs(value))));
|
|
}
|
|
|
|
function other(value: unknown): string {
|
|
if (value === null || value === undefined) return "";
|
|
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
}
|
|
|
|
export function formatNumber(value: unknown): string {
|
|
return typeof value === "number" ? fixed(value, numberPlaces(value)) : other(value);
|
|
}
|
|
|
|
export function formatMoney(value: unknown): string {
|
|
return typeof value === "number" ? fixed(value, 0) : other(value);
|
|
}
|
|
|
|
export function formatLineMoney(value: unknown): string {
|
|
return typeof value === "number" ? fixed(value, 1) : other(value);
|
|
}
|
|
|
|
export function formatPrice(value: unknown): string {
|
|
if (typeof value !== "number") return other(value);
|
|
return Math.abs(value) < 200 ? formatNumber(value) : fixed(value, 3);
|
|
}
|