728x90
반응형
import java.math.BigDecimal;
public class NumberToKorean {
public static String convertToKorean(BigDecimal amount) {
if (amount == null || amount.compareTo(BigDecimal.ZERO) == 0) {
return "영";
}
final String[] KOREAN_NUMBERS = { "", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" };
final String[] UNITS = { "", "십", "백", "천" };
final String[] LARGE_UNITS = { "", "만", "억", "조", "경" };
StringBuilder result = new StringBuilder();
int unitIndex = 0;
// 금액을 정수형으로 처리
long intPart = amount.longValue(); // 소수점을 제거하고 정수형으로 변환
while (intPart > 0) {
int part = (int) (intPart % 10000); // 금액을 4자리씩 분리
if (part != 0) {
StringBuilder partResult = new StringBuilder();
int unitPos = 0;
while (part > 0) {
int digit = part % 10;
if (digit != 0) {
partResult.insert(0, KOREAN_NUMBERS[digit] + UNITS[unitPos]);
}
part /= 10;
unitPos++;
}
result.insert(0, partResult.append(LARGE_UNITS[unitIndex]));
}
intPart /= 10000; // 4자리씩 나누어 처리
unitIndex++;
}
return result.toString().replaceAll("일십", "십"); // "일십" -> "십" 간소화
}
public static void main(String[] args) {
System.out.println("금액: 123456789 -> " + convertToKorean(new BigDecimal("123456789")));
System.out.println("금액: 100200300 -> " + convertToKorean(new BigDecimal("100200300")));
System.out.println("금액: 0 -> " + convertToKorean(new BigDecimal("0")));
System.out.println("금액: null -> " + convertToKorean(null));
}
}728x90
반응형
'JAVA' 카테고리의 다른 글
| [JAVA] JWT인증 토큰을 거치지 않는 예외 API 설정하기 방법2 (0) | 2024.11.23 |
|---|---|
| [JAVA] JWT인증 토큰을 거치지 않는 예외 API 설정하기 방법1 (0) | 2024.11.22 |
| [JAVA] 이미지 파일을 Base64로 변환하여 전송 (0) | 2024.11.18 |
| [JAVA] Stream으로 List 중복 제거 (0) | 2024.11.17 |
| [JAVA] BigDecimal 빼기 (0) | 2024.11.16 |