Files
frontend/apps/as/src/widgets/pbadms-sprt/lib/format-utils.ts
T

40 lines
1.2 KiB
TypeScript

export function fmtNumber(value: number | string | null | undefined): string {
if (typeof value === "number") {
return value.toLocaleString();
}
if (value === null || value === undefined) {
return "";
}
const num = Number(value);
if (!isNaN(num)) {
return num.toLocaleString();
}
return String(value);
}
export function indentPx(indNm: string | undefined): number {
if (!indNm) return 0;
const spaces = indNm.length - indNm.trimStart().length;
return (spaces / 4) * 16;
}
const AMOUNT_DECIMAL_PLACES = 2;
export function roundAmount(value: number, decimals: number = AMOUNT_DECIMAL_PLACES): number {
const factor = 10 ** decimals;
return Math.round((value + Number.EPSILON) * factor) / factor;
}
/** 입력 문자열의 소수부를 decimals 자리까지 잘라낸다 */
export function truncateDecimals(value: string, decimals: number): string {
const dotIndex = value.indexOf(".");
if (dotIndex < 0) return value;
if (decimals <= 0) return value.slice(0, dotIndex);
return value.slice(0, dotIndex + 1 + decimals);
}
export function calcDiffPercent(diff: number, baseline: number): string {
if (baseline === 0) return "-";
return `${Math.round((diff / baseline) * 100)}%`;
}