forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyUtils.java
More file actions
299 lines (284 loc) · 8.38 KB
/
MoneyUtils.java
File metadata and controls
299 lines (284 loc) · 8.38 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package com.JUtils.base;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* 金钱处理工具类
*
* @Author:chenssy
* @date:2014年8月7日
*/
public class MoneyUtils {
/**
* 汉语中数字大写
*/
private static final String[] CN_UPPER_NUMBER = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
/**
* 汉语中货币单位大写
*/
private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元","拾", "佰", "仟", "万", "拾",
"佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
"佰", "仟" };
/**
* 特殊字符:整
*/
private static final String CN_FULL = "";
/**
* 特殊字符:负
*/
private static final String CN_NEGATIVE = "负";
/**
* 零元整
*/
private static final String CN_ZEOR_FULL = "零元整";
/**
* 金额的精度,默认值为2
*/
private static final int MONEY_PRECISION = 2;
/**
* 人民币转换为大写,格式为:x万x千x百x十x元x角x分
*
* @autor:chenssy
* @date:2014年8月7日
*
* @param numberOfMoney 传入的金额
* @return
*/
public static String number2CNMontray(String numberOfMoney) {
return number2CNMontray(new BigDecimal(numberOfMoney));
}
/**
* 人民币转换为大写,格式为:x万x千x百x十x元x角x分
* @autor:chenssy
* @date:2014年8月7日
*
* @param numberOfMoney
* 传入的金额
* @return
*/
public static String number2CNMontray(BigDecimal numberOfMoney) {
StringBuffer sb = new StringBuffer();
int signum = numberOfMoney.signum();
// 零元整的情况
if (signum == 0) {
return CN_ZEOR_FULL;
}
//这里会进行金额的四舍五入
long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();
// 得到小数点后两位值
long scale = number % 100;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
// 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
if (!(scale > 0)) {
numIndex = 2;
number = number / 100;
getZero = true;
}
if ((scale > 0) && (!(scale % 10 > 0))) {
numIndex = 1;
number = number / 10;
getZero = true;
}
int zeroSize = 0;
while (true) {
if (number <= 0) {
break;
}
// 每次获取到最后一个数
numUnit = (int) (number % 10);
if (numUnit > 0) {
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
} else {
++zeroSize;
if (!(getZero)) {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
if (numIndex == 2) {
if (number > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
getZero = true;
}
// 让number每次都去掉最后一个数
number = number / 10;
++numIndex;
}
// 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
// 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
if (!(scale > 0)) {
sb.append(CN_FULL);
}
return sb.toString();
}
/**
* 将人民币转换为会计格式金额(xxxx,xxxx,xxxx.xx),保留两位小数
* @autor:chenssy
* @date:2014年8月7日
*
* @param money
* 待转换的金额
* @return
*/
public static String accountantMoney(BigDecimal money){
return accountantMoney(money, 2, 1);
}
/**
* 格式化金额,显示为xxx万元,xxx百万,xxx亿
* @autor:chenssy
* @date:2014年8月7日
*
* @param money
* 待处理的金额
* @param scale
* 小数点后保留的位数
* @param divisor
* 格式化值(10:十元、100:百元,1000千元,10000万元......)
* @return
*/
public static String getFormatMoney(BigDecimal money,int scale,double divisor){
return formatMoney(money, scale, divisor) + getCellFormat(divisor);
}
/**
* 获取会计格式的人民币(格式为:xxxx,xxxx,xxxx.xx)
* @autor:chenssy
* @date:2014年8月7日
*
* @param money
* 待处理的金额
* @param scale
* 小数点后保留的位数
* @param divisor
* 格式化值(10:十元、100:百元,1000千元,10000万元......)
* @return
*/
public static String getAccountantMoney(BigDecimal money, int scale, double divisor){
return accountantMoney(money, scale, divisor) + getCellFormat(divisor);
}
/**
* 将人民币转换为会计格式金额(xxxx,xxxx,xxxx.xx)
* @autor:chenssy
* @date:2014年8月7日
*
* @param money
* 待处理的金额
* @param scale
* 小数点后保留的位数
* @param divisor
* 格式化值
* @return
*/
private static String accountantMoney(BigDecimal money,int scale,double divisor){
String disposeMoneyStr = formatMoney(money, scale, divisor);
//小数点处理
int dotPosition = disposeMoneyStr.indexOf(".");
String exceptDotMoeny = null;//小数点之前的字符串
String dotMeony = null;//小数点之后的字符串
if(dotPosition > 0){
exceptDotMoeny = disposeMoneyStr.substring(0,dotPosition);
dotMeony = disposeMoneyStr.substring(dotPosition);
}else{
exceptDotMoeny = disposeMoneyStr;
}
//负数处理
int negativePosition = exceptDotMoeny.indexOf("-");
if(negativePosition == 0){
exceptDotMoeny = exceptDotMoeny.substring(1);
}
StringBuffer reverseExceptDotMoney = new StringBuffer(exceptDotMoeny);
reverseExceptDotMoney.reverse();//字符串倒转
char[] moneyChar = reverseExceptDotMoney.toString().toCharArray();
StringBuffer returnMeony = new StringBuffer();//返回值
for(int i = 0; i < moneyChar.length; i++){
if(i != 0 && i % 3 == 0){
returnMeony.append(",");//每隔3位加','
}
returnMeony.append(moneyChar[i]);
}
returnMeony.reverse();//字符串倒转
if(dotPosition > 0){
returnMeony.append(dotMeony);
}
if(negativePosition == 0){
return "-" + returnMeony.toString();
}else{
return returnMeony.toString();
}
}
/**
* 格式化金额,显示为xxx万元,xxx百万,xxx亿
* @autor:chenssy
* @date:2014年8月7日
*
* @param money
* 待处理的金额
* @param scale
* 小数点后保留的位数
* @param divisor
* 格式化值
* @return
*/
private static String formatMoney(BigDecimal money,int scale,double divisor){
if (divisor == 0) {
return "0.00";
}
if (scale < 0) {
return "0.00";
}
BigDecimal divisorBD = new BigDecimal(divisor);
return money.divide(divisorBD, scale, RoundingMode.HALF_UP).toString();
}
private static String getCellFormat(double divisor){
String str = String.valueOf(divisor);
int len = str.substring(0,str.indexOf(".")).length();
String cell = "";
switch(len){
case 1:
cell = "元";
break;
case 2:
cell = "十元";
break;
case 3:
cell = "百元";
break;
case 4:
cell = "千元";
break;
case 5:
cell = "万元";
break;
case 6:
cell = "十万元";
break;
case 7:
cell = "百万元";
break;
case 8:
cell = "千万元";
break;
case 9:
cell = "亿元";
break;
case 10:
cell = "十亿元";
break;
}
return cell;
}
}