摘自 http://www.blogjava.net/hispark/archive/2008/11/19/241321.html
我们在制作单证或报表时,客户经常要我们把最后的合计数转写中文大写金额。这个需求很合理,但感觉并不容易实现,如何在JasperReport中加入大写金额的实现呢?提供一种实现的方法给大家参考。
实现思路:
在报表执行过程中使用scirptlet将存放着数字金额的变量读出转换成大写金额字符串后放入大写金额变量中。报表即可象显示普通字符变量一样显示大写金额。
TransChineseMoneyScriptlet.java代码
1
/** */
/**
2
* 大写金额转换Scriptlet类
3
*
4
* @author Spark (Email: spark.unt@gmail.com)
5
*/
6
public
class
TransChineseMoneyScriptlet
extends
JRDefaultScriptlet
{
7
/**/ /*
8
* 默认构造方法
9
*/
10
public TransChineseMoneyScriptlet()
{
11
12
}
13
14
/** */ /**
15
* 获得金额的汉字大写格式 <br>
16
* @param money 小写金额字符串
17
* @return 大写的汉字金额
18
*/
19
public static String getChineseMoney(String money)
{
20
String text = transChineseMoney1(money) + transChineseMoney2(money);
21
Pattern p = Pattern.compile( " 零分 " , Pattern.CASE_INSENSITIVE);
22
Matcher m = p.matcher(text);
23
text = m.replaceAll( "" );
24
return text;
25
}
26
27
/** */ /**
28
* 截得输入金额的整数部分,并将其转换成中文大写的格式 <br>
29
* <br>
30
* 其他描述:输入数字超过接受范围时拒绝转换并退出。<br>
31
* @param 传递参数字符串S 参数描述
32
* @return 返回转换后的字符串
33
*/
34
public static String transChineseMoney1(String s)
{
35
String ss = s;
36
String tmpnewchar = "" ;
37
String[] part = ss.split( " \\. " );
38
39
if (part[ 0 ].length() > 10 )
{
40
// 超出可转换位数
41
return "" ;
42
}
43
for ( int i = 0 ; i < part[ 0 ].length(); i ++ )
{
44
char perchar = part[ 0 ].charAt(i);
45
if (perchar == ' 0 ' )
46
tmpnewchar = tmpnewchar + " 零 " ;
47
if (perchar == ' 1 ' )
48
tmpnewchar = tmpnewchar + " 壹 " ;
49
if (perchar == ' 2 ' )
50
tmpnewchar = tmpnewchar + " 贰 " ;
51
if (perchar == ' 3 ' )
52
tmpnewchar = tmpnewchar + " 叁 " ;
53
if (perchar == ' 4 ' )
54
tmpnewchar = tmpnewchar + " 肆 " ;
55
if (perchar == ' 5 ' )
56
tmpnewchar = tmpnewchar + " 伍 " ;
57
if (perchar == ' 6 ' )
58
tmpnewchar = tmpnewchar + " 陆 " ;
59
if (perchar == ' 7 ' )
60
tmpnewchar = tmpnewchar + " 柒 " ;
61
if (perchar == ' 8 ' )
62
tmpnewchar = tmpnewchar + " 捌 " ;
63
if (perchar == ' 9 ' )
64
tmpnewchar = tmpnewchar + " 玖 " ;
65
66
int j = part[ 0 ].length() - i - 1 ;
67
if (j == 0 )
68
tmpnewchar = tmpnewchar + " 圆 " ;
69
if (j == 1 && perchar != 0 )
70
tmpnewchar = tmpnewchar + " 拾 " ;
71
if (j == 2 && perchar != 0 )
72
tmpnewchar = tmpnewchar + " 佰 " ;
73
if (j == 3 && perchar != 0 )
74
tmpnewchar = tmpnewchar + " 仟 " ;
75
if (j == 4 && perchar != 0 )
76
tmpnewchar = tmpnewchar + " 万 " ;
77
if (j == 5 && perchar != 0 )
78
tmpnewchar = tmpnewchar + " 拾 " ;
79
if (j == 6 && perchar != 0 )
80
tmpnewchar = tmpnewchar + " 佰 " ;
81
if (j == 7 && perchar != 0 )
82
tmpnewchar = tmpnewchar + " 仟 " ;
83
if (j == 8 && perchar != 0 )
84
tmpnewchar = tmpnewchar + " 亿 " ;
85
if (j == 9 && perchar != 0 )
86
tmpnewchar = tmpnewchar + " 拾 " ;
87
}
88
return tmpnewchar;
89
}
90
91
/** */ /**
92
* 截得输入金额的小数部分,并将其转换成中文大写的格式 <br>
93
* <br>
94
* 其他描述:小数部分超过两位时系统自动截断。<br>
95
*
96
* @param 传递参数字符串
97
*
98
* @return 返回转换后的字符串
99
*/
100
public static String transChineseMoney2(String s)
{
101
String ss = s;
102
String tmpnewchar1 = "" ;
103
String[] part = ss.split( " \\. " );
104
105
if (ss.indexOf( " . " ) != - 1 )
{
106
if (part[ 1 ].length() > 2 )
{
107
// MessageDialog.openInformation(null,"提示","小数点之后只能保留两位,系统将自动截段");
108
part[ 1 ] = part[ 1 ].substring( 0 , 2 );
109
}
110
for ( int i = 0 ; i < part[ 1 ].length(); i ++ )
{
111
char perchar = part[ 1 ].charAt(i);
112
// System.out.println(perchar);
113
if (perchar == ' 0 ' )
114
tmpnewchar1 = tmpnewchar1 + " 零 " ;
115
if (perchar == ' 1 ' )
116
tmpnewchar1 = tmpnewchar1 + " 壹 " ;
117
if (perchar == ' 2 ' )
118
tmpnewchar1 = tmpnewchar1 + " 贰 " ;
119
if (perchar == ' 3 ' )
120
tmpnewchar1 = tmpnewchar1 + " 叁 " ;
121
if (perchar == ' 4 ' )
122
tmpnewchar1 = tmpnewchar1 + " 肆 " ;
123
if (perchar == ' 5 ' )
124
tmpnewchar1 = tmpnewchar1 + " 伍 " ;
125
if (perchar == ' 6 ' )
126
tmpnewchar1 = tmpnewchar1 + " 陆 " ;
127
if (perchar == ' 7 ' )
128
tmpnewchar1 = tmpnewchar1 + " 柒 " ;
129
if (perchar == ' 8 ' )
130
tmpnewchar1 = tmpnewchar1 + " 捌


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
