code
1
package
test.format;
2 import java.text.NumberFormat;
3 import java.util.HashMap;
4 public class SimpleMoneyFormat {
5 public static final String EMPTY = "" ;
6 public static final String ZERO = " 零 " ;
7 public static final String ONE = " 壹 " ;
8 public static final String TWO = " 贰 " ;
9 public static final String THREE = " 叁 " ;
10 public static final String FOUR = " 肆 " ;
11 public static final String FIVE = " 伍 " ;
12 public static final String SIX = " 陆 " ;
13 public static final String SEVEN = " 柒 " ;
14 public static final String EIGHT = " 捌 " ;
15 public static final String NINE = " 玖 " ;
16 public static final String TEN = " 拾 " ;
17 public static final String HUNDRED = " 佰 " ;
18 public static final String THOUSAND = " 仟 " ;
19 public static final String TEN_THOUSAND = " 万 " ;
20 public static final String HUNDRED_MILLION = " 亿 " ;
21 public static final String YUAN = " 元 " ;
22 public static final String JIAO = " 角 " ;
23 public static final String FEN = " 分 " ;
24 public static final String DOT = " . " ;
25
26 private static SimpleMoneyFormat formatter = null ;
27 private HashMap chineseNumberMap = new HashMap();
28 private HashMap chineseMoneyPattern = new HashMap();
29 private NumberFormat numberFormat = NumberFormat.getInstance ();
30
31 private SimpleMoneyFormat() {
32 numberFormat.setMaximumFractionDigits( 4 );
33 numberFormat.setMinimumFractionDigits( 2 );
34 numberFormat.setGroupingUsed( false );
35
36 chineseNumberMap.put( " 0 " , ZERO);
37 chineseNumberMap.put( " 1 " , ONE);
38 chineseNumberMap.put( " 2 " , TWO);
39 chineseNumberMap.put( " 3 " , THREE);
40 chineseNumberMap.put( " 4 " , FOUR);
41 chineseNumberMap.put( " 5 " , FIVE);
42 chineseNumberMap.put( " 6 " , SIX);
43 chineseNumberMap.put( " 7 " , SEVEN);
44 chineseNumberMap.put( " 8 " , EIGHT);
45 chineseNumberMap.put( " 9 " , NINE);
46 chineseNumberMap.put(DOT, DOT);
47
48 chineseMoneyPattern.put( " 1 " , TEN);
49 chineseMoneyPattern.put( " 2 " , HUNDRED);
50 chineseMoneyPattern.put( " 3 " , THOUSAND);
51 chineseMoneyPattern.put( " 4 " , TEN_THOUSAND);
52 chineseMoneyPattern.put( " 5 " , TEN);
53 chineseMoneyPattern.put( " 6 " , HUNDRED);
54 chineseMoneyPattern.put( " 7 " , THOUSAND);
55 chineseMoneyPattern.put( " 8 " , HUNDRED_MILLION);
56 }
57
58 public static SimpleMoneyFormat getInstance() {
59 if (formatter == null )
60 formatter = new SimpleMoneyFormat();
61 return formatter;
62 }
63
64 public String format(String moneyStr) {
65 checkPrecision(moneyStr);
66 String result;
67 result = convertToChineseNumber(moneyStr);
68 result = addUnitsToChineseMoneyString(result);
69 return result;
70 }
71
72 public String format( double moneyDouble) {
73 return format(numberFormat.format(moneyDouble));
74 }
75
76 public String format( int moneyInt) {
77 return format(numberFormat.format(moneyInt));
78 }
79
80 public String format( long moneyLong) {
81 return format(numberFormat.format(moneyLong));
82 }
83
84 public String format(Number moneyNum) {
85 return format(numberFormat.format(moneyNum));
86 }
87
88 private String convertToChineseNumber(String moneyStr) {
89 String result;
90 StringBuffer cMoneyStringBuffer = new StringBuffer();
91 for ( int i = 0 ; i < moneyStr.length(); i ++ ) {
92 cMoneyStringBuffer.append (chineseNumberMap.get(moneyStr.substring(i, i + 1 )));
93 }
94 // 拾佰仟万亿等都是汉字里面才有的单位,加上它们
95 int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
96 int moneyPatternCursor = 1 ;
97 for ( int i = indexOfDot - 1 ; i > 0 ; i -- ) {
98 cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));
99 moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1 ;
100 }
101
102 String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf( " . " ));
103 cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf( " . " ), cMoneyStringBuffer.length());
104 while ( cMoneyStringBuffer.indexOf( " 零拾 " ) != - 1 ) {
105 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf( " 零拾 " ), cMoneyStringBuffer.indexOf( " 零拾 " ) + 2 , ZERO);
106 }
107 while (cMoneyStringBuffer.indexOf ( " 零佰 " ) != - 1 ) {
108 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf( " 零佰 " ), cMoneyStringBuffer.indexOf( " 零佰 " ) + 2 , ZERO);
109 }
110 while (cMoneyStringBuffer.indexOf( " 零仟 " ) != - 1 ) {
111 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf( " 零仟 " ), cMoneyStringBuffer.indexOf( " 零仟 " ) + 2 , ZERO);
112 }
113 while (cMoneyStringBuffer.indexOf( " 零万 " ) != - 1 ) {
114 cMoneyStringBuffer.replace (cMoneyStringBuffer.indexOf( " 零万 " ), cMoneyStringBuffer.indexOf( " 零万 " ) + 2 , TEN_THOUSAND);
115 }
116 while (cMoneyStringBuffer.indexOf( " 零亿 " ) != - 1 ) {
117 cMoneyStringBuffer.replace( cMoneyStringBuffer.indexOf ( " 零亿 " ), cMoneyStringBuffer.indexOf( " 零亿 " ) + 2 , HUNDRED_MILLION);
118 }
119 while (cMoneyStringBuffer.indexOf( " 零零 " ) != - 1 ) {
120 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf ( " 零零 " ), cMoneyStringBuffer.indexOf( " 零零 " ) + 2 , ZERO);
121 }
122 if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1 )
123 cMoneyStringBuffer.delete(cMoneyStringBuffer.length () - 1 , cMoneyStringBuffer.length());
124 cMoneyStringBuffer.append(fractionPart);
125
126 result = cMoneyStringBuffer.toString();
127 return result;
128 }
129
130
131 private String addUnitsToChineseMoneyString(String moneyStr) {
132 String result;
133 StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
134 int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
135 cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1 , YUAN);
2 import java.text.NumberFormat;
3 import java.util.HashMap;
4 public class SimpleMoneyFormat {
5 public static final String EMPTY = "" ;
6 public static final String ZERO = " 零 " ;
7 public static final String ONE = " 壹 " ;
8 public static final String TWO = " 贰 " ;
9 public static final String THREE = " 叁 " ;
10 public static final String FOUR = " 肆 " ;
11 public static final String FIVE = " 伍 " ;
12 public static final String SIX = " 陆 " ;
13 public static final String SEVEN = " 柒 " ;
14 public static final String EIGHT = " 捌 " ;
15 public static final String NINE = " 玖 " ;
16 public static final String TEN = " 拾 " ;
17 public static final String HUNDRED = " 佰 " ;
18 public static final String THOUSAND = " 仟 " ;
19 public static final String TEN_THOUSAND = " 万 " ;
20 public static final String HUNDRED_MILLION = " 亿 " ;
21 public static final String YUAN = " 元 " ;
22 public static final String JIAO = " 角 " ;
23 public static final String FEN = " 分 " ;
24 public static final String DOT = " . " ;
25
26 private static SimpleMoneyFormat formatter = null ;
27 private HashMap chineseNumberMap = new HashMap();
28 private HashMap chineseMoneyPattern = new HashMap();
29 private NumberFormat numberFormat = NumberFormat.getInstance ();
30
31 private SimpleMoneyFormat() {
32 numberFormat.setMaximumFractionDigits( 4 );
33 numberFormat.setMinimumFractionDigits( 2 );
34 numberFormat.setGroupingUsed( false );
35
36 chineseNumberMap.put( " 0 " , ZERO);
37 chineseNumberMap.put( " 1 " , ONE);
38 chineseNumberMap.put( " 2 " , TWO);
39 chineseNumberMap.put( " 3 " , THREE);
40 chineseNumberMap.put( " 4 " , FOUR);
41 chineseNumberMap.put( " 5 " , FIVE);
42 chineseNumberMap.put( " 6 " , SIX);
43 chineseNumberMap.put( " 7 " , SEVEN);
44 chineseNumberMap.put( " 8 " , EIGHT);
45 chineseNumberMap.put( " 9 " , NINE);
46 chineseNumberMap.put(DOT, DOT);
47
48 chineseMoneyPattern.put( " 1 " , TEN);
49 chineseMoneyPattern.put( " 2 " , HUNDRED);
50 chineseMoneyPattern.put( " 3 " , THOUSAND);
51 chineseMoneyPattern.put( " 4 " , TEN_THOUSAND);
52 chineseMoneyPattern.put( " 5 " , TEN);
53 chineseMoneyPattern.put( " 6 " , HUNDRED);
54 chineseMoneyPattern.put( " 7 " , THOUSAND);
55 chineseMoneyPattern.put( " 8 " , HUNDRED_MILLION);
56 }
57
58 public static SimpleMoneyFormat getInstance() {
59 if (formatter == null )
60 formatter = new SimpleMoneyFormat();
61 return formatter;
62 }
63
64 public String format(String moneyStr) {
65 checkPrecision(moneyStr);
66 String result;
67 result = convertToChineseNumber(moneyStr);
68 result = addUnitsToChineseMoneyString(result);
69 return result;
70 }
71
72 public String format( double moneyDouble) {
73 return format(numberFormat.format(moneyDouble));
74 }
75
76 public String format( int moneyInt) {
77 return format(numberFormat.format(moneyInt));
78 }
79
80 public String format( long moneyLong) {
81 return format(numberFormat.format(moneyLong));
82 }
83
84 public String format(Number moneyNum) {
85 return format(numberFormat.format(moneyNum));
86 }
87
88 private String convertToChineseNumber(String moneyStr) {
89 String result;
90 StringBuffer cMoneyStringBuffer = new StringBuffer();
91 for ( int i = 0 ; i < moneyStr.length(); i ++ ) {
92 cMoneyStringBuffer.append (chineseNumberMap.get(moneyStr.substring(i, i + 1 )));
93 }
94 // 拾佰仟万亿等都是汉字里面才有的单位,加上它们
95 int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
96 int moneyPatternCursor = 1 ;
97 for ( int i = indexOfDot - 1 ; i > 0 ; i -- ) {
98 cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));
99 moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1 ;
100 }
101
102 String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf( " . " ));
103 cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf( " . " ), cMoneyStringBuffer.length());
104 while ( cMoneyStringBuffer.indexOf( " 零拾 " ) != - 1 ) {
105 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf( " 零拾 " ), cMoneyStringBuffer.indexOf( " 零拾 " ) + 2 , ZERO);
106 }
107 while (cMoneyStringBuffer.indexOf ( " 零佰 " ) != - 1 ) {
108 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf( " 零佰 " ), cMoneyStringBuffer.indexOf( " 零佰 " ) + 2 , ZERO);
109 }
110 while (cMoneyStringBuffer.indexOf( " 零仟 " ) != - 1 ) {
111 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf( " 零仟 " ), cMoneyStringBuffer.indexOf( " 零仟 " ) + 2 , ZERO);
112 }
113 while (cMoneyStringBuffer.indexOf( " 零万 " ) != - 1 ) {
114 cMoneyStringBuffer.replace (cMoneyStringBuffer.indexOf( " 零万 " ), cMoneyStringBuffer.indexOf( " 零万 " ) + 2 , TEN_THOUSAND);
115 }
116 while (cMoneyStringBuffer.indexOf( " 零亿 " ) != - 1 ) {
117 cMoneyStringBuffer.replace( cMoneyStringBuffer.indexOf ( " 零亿 " ), cMoneyStringBuffer.indexOf( " 零亿 " ) + 2 , HUNDRED_MILLION);
118 }
119 while (cMoneyStringBuffer.indexOf( " 零零 " ) != - 1 ) {
120 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf ( " 零零 " ), cMoneyStringBuffer.indexOf( " 零零 " ) + 2 , ZERO);
121 }
122 if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1 )
123 cMoneyStringBuffer.delete(cMoneyStringBuffer.length () - 1 , cMoneyStringBuffer.length());
124 cMoneyStringBuffer.append(fractionPart);
125
126 result = cMoneyStringBuffer.toString();
127 return result;
128 }
129
130
131 private String addUnitsToChineseMoneyString(String moneyStr) {
132 String result;
133 StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
134 int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
135 cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1 , YUAN);