概述
代码
import java. math. BigDecimal ;
import java. util. Scanner ; public class AmountToChinese { private static final String [ ] NUMBERS = { "零" , "壹" , "贰" , "叁" , "肆" , "伍" , "陆" , "柒" , "捌" , "玖" } ; private static final String [ ] UNITS = { "" , "拾" , "佰" , "仟" , "万" , "拾" , "佰" , "仟" , "亿" , "拾" , "佰" , "仟" } ; private static final String [ ] DECIMAL_UNITS = { "角" , "分" } ; public static void main ( String [ ] args) { Scanner scanner = new Scanner ( System . in) ; System . out. print ( "请输入金额(支持两位小数):" ) ; String amountStr = scanner. nextLine ( ) ; scanner. close ( ) ; try { String chineseAmount = convertToChinese ( amountStr) ; System . out. println ( "中文大写:" + chineseAmount) ; } catch ( IllegalArgumentException e) { System . out. println ( "输入错误:" + e. getMessage ( ) ) ; } } public static String convertToChinese ( String amountStr) { if ( amountStr == null || amountStr. trim ( ) . isEmpty ( ) ) { throw new IllegalArgumentException ( "金额不能为空" ) ; } amountStr = amountStr. trim ( ) . replaceAll ( "\\s+" , "" ) ; if ( ! amountStr. matches ( "^\\d+(\\.\\d{1,2})?$" ) ) { throw new IllegalArgumentException ( "金额格式不正确,最多支持两位小数" ) ; } if ( amountStr. equals ( "0" ) || amountStr. equals ( "0.0" ) || amountStr. equals ( "0.00" ) ) { return "零元整" ; } String [ ] parts = amountStr. split ( "\\." ) ; String integerPart = parts[ 0 ] ; String decimalPart = parts. length > 1 ? parts[ 1 ] : "" ; if ( decimalPart. length ( ) == 1 ) { decimalPart += "0" ; } else if ( decimalPart. length ( ) > 2 ) { decimalPart = decimalPart. substring ( 0 , 2 ) ; } StringBuilder result = new StringBuilder ( ) ; if ( ! integerPart. equals ( "0" ) ) { result. append ( convertIntegerPart ( integerPart) ) ; result. append ( "元" ) ; } if ( ! decimalPart. isEmpty ( ) && ! decimalPart. equals ( "00" ) ) { result. append ( convertDecimalPart ( decimalPart) ) ; } else { result. append ( "整" ) ; } return result. toString ( ) ; } private static String convertIntegerPart ( String integerPart) { StringBuilder result = new StringBuilder ( ) ; int length = integerPart. length ( ) ; for ( int i = 0 ; i < length; i++ ) { int digit = integerPart. charAt ( i) - '0' ; int position = length - i - 1 ; if ( digit == 0 ) { if ( i < length - 1 && integerPart. charAt ( i + 1 ) != '0' && ! result. toString ( ) . endsWith ( "零" ) ) { result. append ( "零" ) ; } if ( position % 4 == 0 ) { result. append ( UNITS [ position] ) ; } } else { result. append ( NUMBERS [ digit] ) ; result. append ( UNITS [ position] ) ; } } String resultStr = result. toString ( ) ; if ( resultStr. startsWith ( "壹拾" ) && resultStr. length ( ) > 2 ) { resultStr = resultStr. substring ( 1 ) ; } return resultStr; } private static String convertDecimalPart ( String decimalPart) { StringBuilder result = new StringBuilder ( ) ; int jiao = decimalPart. charAt ( 0 ) - '0' ; int fen = decimalPart. charAt ( 1 ) - '0' ; if ( jiao != 0 ) { result. append ( NUMBERS [ jiao] ) . append ( DECIMAL_UNITS [ 0 ] ) ; } if ( fen != 0 ) { if ( jiao == 0 ) { result. append ( "零" ) ; } result. append ( NUMBERS [ fen] ) . append ( DECIMAL_UNITS [ 1 ] ) ; } return result. toString ( ) ; }
}