Files
superport/lib/utils/currency_formatter.dart
JiWoong Sul c141c0b914 feat: Equipment DTO 호환성 수정 전 백업 커밋
- Equipment DTO 필드명 변경 (name → equipment_number 등) 완료
- Phase 1-7 파생 수정사항 체계적 진행 예정
- 통합 모델 정리, Controller 동기화, UI 업데이트 예정

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-21 19:17:43 +09:00

64 lines
1.8 KiB
Dart

import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
/// KRW 통화 포맷팅 관련 유틸리티
class CurrencyFormatter {
static final NumberFormat _formatter = NumberFormat('#,###', 'ko_KR');
/// 숫자를 KRW 통화 형식으로 포맷팅 (₩2,000,000)
static String formatKRW(double? value) {
if (value == null || value == 0) return '';
return '${_formatter.format(value.toInt())}';
}
/// 통화 포맷팅된 문자열에서 숫자만 추출
static double? parseKRW(String? text) {
if (text == null || text.isEmpty) return null;
// ₩, 쉼표 제거하고 숫자만 추출
final cleanText = text.replaceAll(RegExp(r'[₩,]'), '');
return double.tryParse(cleanText);
}
}
/// KRW 통화 입력을 위한 TextInputFormatter
class KRWTextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
// 빈 문자열인 경우 그대로 반환
if (newValue.text.isEmpty) {
return newValue;
}
// 숫자만 추출
final numericText = newValue.text.replaceAll(RegExp(r'[^\d]'), '');
// 숫자가 없으면 빈 문자열
if (numericText.isEmpty) {
return const TextEditingValue(
text: '',
selection: TextSelection.collapsed(offset: 0),
);
}
// 숫자 파싱 및 포맷팅
final numericValue = double.tryParse(numericText);
if (numericValue == null) {
return oldValue;
}
// KRW 포맷팅 적용
final formattedText = CurrencyFormatter.formatKRW(numericValue);
// 커서 위치 계산 (맨 끝으로)
final selectionOffset = formattedText.length;
return TextEditingValue(
text: formattedText,
selection: TextSelection.collapsed(offset: selectionOffset),
);
}
}