summaryrefslogtreecommitdiff
path: root/lib/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.ts')
-rw-r--r--lib/utils.ts45
1 files changed, 44 insertions, 1 deletions
diff --git a/lib/utils.ts b/lib/utils.ts
index dab65b37..98142389 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -236,7 +236,7 @@ export function formatHtml(html: string): string {
return html; // 이미 포맷팅된 것으로 보임
}
- let formatted = html
+ const formatted = html
// 태그 앞뒤 공백 정리
.replace(/>\s*</g, '><')
// 블록 요소들 앞에 줄바꿈
@@ -290,3 +290,46 @@ export function compareItemNumber(a?: string, b?: string) {
return as.length - bs.length;
}
+/**
+ * 숫자를 3자리마다 콤마(,)로 구분하여 포맷팅합니다.
+ *
+ * @param value 포맷팅할 숫자 (number, string, null, undefined 허용)
+ * @param decimals 소수점 자릿수 (선택, 지정하지 않으면 원본 소수점 유지)
+ * @returns 포맷된 문자열 (예: "1,234,567.89")
+ *
+ * @example
+ * formatNumber(1234567) // "1,234,567"
+ * formatNumber(1234567.89) // "1,234,567.89"
+ * formatNumber(1234567.89, 1) // "1,234,567.9"
+ * formatNumber(null) // "-"
+ * formatNumber("1234567") // "1,234,567"
+ */
+export function formatNumber(
+ value: number | string | null | undefined,
+ decimals?: number
+): string {
+ // null, undefined, 빈 문자열 처리
+ if (value === null || value === undefined || value === '') {
+ return '-';
+ }
+
+ // 문자열을 숫자로 변환
+ const numValue = typeof value === 'string' ? parseFloat(value) : value;
+
+ // NaN 체크
+ if (isNaN(numValue)) {
+ return '-';
+ }
+
+ // 소수점 자릿수가 지정된 경우
+ if (decimals !== undefined) {
+ return numValue.toLocaleString('en-US', {
+ minimumFractionDigits: decimals,
+ maximumFractionDigits: decimals,
+ });
+ }
+
+ // 소수점 자릿수가 지정되지 않은 경우 (원본 소수점 유지)
+ return numValue.toLocaleString('en-US');
+}
+