本文實(shí)例為大家分享了Java金額大小寫轉(zhuǎn)換的具體代碼,供大家參考,具體內(nèi)容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/** * @ClassName: NumberConvert * @Description: TODO * @date 2013-3-07 下午16:08:10 * @version V1.0 */ public class MoneyUtil { /** 大寫數(shù)字 */ private static final String[] NUMBERS = { "零" , "壹" , "貳" , "叁" , "肆" , "伍" , "陸" , "柒" , "捌" , "玖" }; /** 整數(shù)部分的單位 */ private static final String[] IUNIT = { "元" , "拾" , "佰" , "仟" , "萬" , "拾" , "佰" , "仟" , "億" , "拾" , "佰" , "仟" , "萬" , "拾" , "佰" , "仟" }; /** 小數(shù)部分的單位 */ private static final String[] DUNIT = { "角" , "分" , "厘" }; /** * 得到大寫金額。 */ public static String toChinese(String str) { str = str.replaceAll( "," , "" ); // 去掉"," String integerStr; // 整數(shù)部分?jǐn)?shù)字 String decimalStr; // 小數(shù)部分?jǐn)?shù)字 // 初始化:分離整數(shù)部分和小數(shù)部分 if (str.indexOf( "." ) > 0 ) { integerStr = str.substring( 0 , str.indexOf( "." )); decimalStr = str.substring(str.indexOf( "." ) + 1 ); } else if (str.indexOf( "." ) == 0 ) { integerStr = "" ; decimalStr = str.substring( 1 ); } else { integerStr = str; decimalStr = "" ; } // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去) if (!integerStr.equals( "" )) { integerStr = Long.toString(Long.parseLong(integerStr)); if (integerStr.equals( "0" )) { integerStr = "" ; } } // overflow超出處理能力,直接返回 if (integerStr.length() > IUNIT.length) { System.out.println(str + ":超出處理能力" ); return str; } int [] integers = toArray(integerStr); // 整數(shù)部分?jǐn)?shù)字 boolean isMust5 = isMust5(integerStr); // 設(shè)置萬單位 int [] decimals = toArray(decimalStr); // 小數(shù)部分?jǐn)?shù)字 return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals); } /** * 整數(shù)部分和小數(shù)部分轉(zhuǎn)換為數(shù)組,從高位至低位 */ private static int [] toArray(String number) { int [] array = new int [number.length()]; for ( int i = 0 ; i < number.length(); i++) { array[i] = Integer.parseInt(number.substring(i, i + 1 )); } return array; } /** * 得到中文金額的整數(shù)部分。 */ private static String getChineseInteger( int [] integers, boolean isMust5) { StringBuffer chineseInteger = new StringBuffer( "" ); int length = integers.length; for ( int i = 0 ; i < length; i++) { // 0出現(xiàn)在關(guān)鍵位置:1234(萬)5678(億)9012(萬)3456(元) // 特殊情況:10(拾元、壹拾元、壹拾萬元、拾萬元) String key = "" ; if (integers[i] == 0 ) { if ((length - i) == 13 ) // 萬(億)(必填) key = IUNIT[ 4 ]; else if ((length - i) == 9 ) // 億(必填) key = IUNIT[ 8 ]; else if ((length - i) == 5 && isMust5) // 萬(不必填) key = IUNIT[ 4 ]; else if ((length - i) == 1 ) // 元(必填) key = IUNIT[ 0 ]; // 0遇非0時補(bǔ)零,不包含最后一位 if ((length - i) > 1 && integers[i + 1 ] != 0 ) key += NUMBERS[ 0 ]; } chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1 ])); } return chineseInteger.toString(); } /** * 得到中文金額的小數(shù)部分。 */ private static String getChineseDecimal( int [] decimals) { StringBuffer chineseDecimal = new StringBuffer( "" ); for ( int i = 0 ; i < decimals.length; i++) { // 舍去3位小數(shù)之后的 if (i == 3 ) break ; chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i])); } return chineseDecimal.toString(); } /** * 判斷第5位數(shù)字的單位"萬"是否應(yīng)加。 */ private static boolean isMust5(String integerStr) { int length = integerStr.length(); if (length > 4 ) { String subInteger = "" ; if (length > 8 ) { // 取得從低位數(shù),第5到第8位的字串 subInteger = integerStr.substring(length - 8 , length - 4 ); } else { subInteger = integerStr.substring( 0 , length - 4 ); } return Integer.parseInt(subInteger) > 0 ; } else { return false ; } } public static void main(String[] args) { String number = "1.23" ; System.out.println(number + " " + MoneyUtil.toChinese(number)); number = "1234567890123456.123" ; System.out.println(number + " " + MoneyUtil.toChinese(number)); number = "0.0798" ; System.out.println(number + " " + MoneyUtil.toChinese(number)); number = "10,001,000.09" ; System.out.println(number + " " + MoneyUtil.toChinese(number)); number = "01.107700" ; System.out.println(number + " " + MoneyUtil.toChinese(number)); } } |
運(yùn)行結(jié)果為:
1.23 壹元貳角叁分
1234567890123456.123 壹仟貳佰叁拾肆萬伍仟陸佰柒拾捌億玖仟零壹拾貳萬叁仟肆佰伍拾陸元壹角貳分叁厘
0.0798 柒分玖厘
10,001,000.09 壹仟萬零壹仟元玖分
01.107700 壹元壹角柒厘
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/ouyang_peng/article/details/8647084