Java application: convert a floating point to an upper-case Chinese amount

Source: Internet
Author: User

Read a floating point value and convert it to the Chinese capital of the amount.

 

Test requirements:

 

When the amount is an integer, it only indicates the integer part, omitting the fractional part, and adding the word "integer.

 

When the amount contains 0 consecutive values, you only need one zero value.

 

The representation of 10. For example, 110 -- one yuan a collection, 10 --- one yuan a collection

 

 

--------------------------------------------------------------------------------

1 import java. Io .*;

2 class chinesemoney ...{

3 private string number [] =... {"," 1 "," 2 "," 3 "," Si "," Wu "," Lu "," Lu ", "success "};

4 private string Unit [] =... {"", "pick up", "success", "success "};

5 private string small [] =... {"", "points "};

6 // Private string strnumber, strunit, strall;

7

8 // whether it is in number

9 private Boolean isinnumber (string strnumber)

10 ...{

11 Boolean innumber = false;

12 For (INT I = 0; I <9; I ++)

13 ...{

14 if (strnumber. compareto (number [I]) = 0) innumber = true;

15}

16 return innumber;

17}

18

19

20 private string splitchinesenumber (INT intunit, string strint)

21 ...{

22 int L = strint. Length ();

23 Int J, K, zeorcounttemp = 0;

24 string strunit = "", strnumber = "", strall = "";

25

26 // determines whether all the digits between 10 million and are 0. If yes, "" is not returned and "is returned.

27 Boolean temp = false;

28 For (k = 0; k29 ...{

30 string strtemp = strint. substring (K, k + 1 );

31 int inttemp = integer. parseint (strtemp );

32

33 If (inttemp! = 0) temp = true;

34}

35 if (temp = false)

36 ...{

37 If (intunit = 5) Return "";

38}

39

40

41 int checkk = 0;

42 // start Conversion

43 for (k = 0; k44 ...{

45 string strtemp = strint. substring (K, k + 1 );

46 int inttemp = integer. parseint (strtemp );

47 strnumber = number [inttemp];

48

49 // J slave

50 J = l-1-k;

51

52 strunit = Unit [J];

53

54

55 // value + Unit

56 // if the value is 0, the value is ""

57 if (inttemp = 0)

58 ...{

59 //

60 if (zeorcounttemp = 0)

61 ...{

62 // unit = zero

63 strunit = strunit. replace (/'pick up/',/'/'zero /'/'/'/');

64 strunit = strunit. replace (/'/'success/',/'/'zero /'/'/'/');

65 strunit = strunit. replace (/'/'success/',/'/'zero /'/'/'/');

66}

67 else

68 ...{

69 // in case of multiple zeros, the unit is ""

70 strunit = strunit. replaceall ("pick up ","");

71 strunit = strunit. replaceall ("success ","");

72 strunit = strunit. replaceall ("success ","");

73}

74 zeorcounttemp ++;

75}

76 checkk = K;

77 strall + = strnumber + strunit;

78}

79

80 return strall;

81}

82

83 private string onlyint (INT intint)

84 ...{

85 string strint;

86 strint = string. valueof (intint );

87 int L = strint. Length ();

88

89 string strall = "";

90 // separate by four digits and one digit

91 If (L> 8) // million

92 ...{

93 strall + = This. splitchinesenumber (9, strint. substring (0, L-8) + "";

94 strall + = This. splitchinesenumber (5, strint. substring (L-8, L-4 ));

95 strall + = This. splitchinesenumber (1, strint. substring (L-4, L) + "Yuan ";

96}

97 else if (L> 4) // 10 thousand

98 ...{

99 strall + = This. splitchinesenumber (5, strint. substring (0, L-4 ));

100 strall + = This. splitchinesenumber (1, strint. substring (L-4, L) + "Yuan ";

101

102}

103 else if (L> 0)

104 ...{

105 strall + = This. splitchinesenumber (1, strint) + "RMB ";

106}

107 //

108 //

109 //

110 //

111 // 100101000

112 int checkl = strall. Length ();

113

114 char strtemp2;

115 for (int K = 1; k116 ...{

117 strtemp2 = strall. charat (k );

118 If (strtemp2 =/'/'zero /'/'/'/')

119 ...{

120 // determines whether there is a number before and after Zero. If there is no number, delete this zero.

121 string strbeforetemp = strall. substring (K-1, k );

122 string straftertemp = strall. substring (k + 1, K + 2 );

123 If (! This. isinnumber (strbeforetemp )&&! This. isinnumber (straftertemp ))

124 ...{

125 strbeforetemp = strall. substring (0, k );

126 straftertemp = strall. substring (k + 1, checkl );

127 strall = strbeforetemp + straftertemp;

128 break;

129}

130

131}

132}

133

134 return strall;

135

136}

137

138 private string onlysmall (INT intsmall)

139 ...{

140 string strnumber, strunit, strall;

141 strnumber = ""; strunit = ""; strall = "";

142 string strsmall, strtemp;

143 strsmall = string. valueof (intsmall );

144 int I;

145 If (intsmall> = 10)

146 ...{

147 for (I = 0; i148 ...{

149 strtemp = string. valueof (intsmall). substring (I, I + 1 );

150 if (integer. parseint (strtemp )! = 0)

151 ...{

152 strnumber = number [integer. parseint (strtemp)];

153 strunit = Small [I];

154 strall + = strnumber + strunit;

155}

156}

157}

158 else

159 ...{

160 if (intsmall! = 0)

161 ...{

162 strnumber = number [intsmall];

163 strunit = Small [1];

164 strall + = strnumber + strunit;

165}

166}

167

168 return strall;

169}

170

171 Public String getchinesemoney (double number)

172 ...{

173 // rounding

174 Number = (Number * 100 + 0.5)/100;

175

176 string strall, strchineseint, strchinesesmall, strzheng ;;

177 int intint, intsmall;

178 strchineseint = ""; strchinesesmall = ""; strzheng = "";

179

180 // integer

181 intint = (INT) (Number * 100/100 );

182 If (intint! = 0)

183 ...{

184 strchineseint = onlyint (intint );

185}

186 // decimal part

187 double temp = (number-intint) x 100*100/100;

188 // rounding the fractional part

189 intsmall = (INT) (temp * 100 + 0.5)/100;

190 If (intsmall! = 0)

191 ...{

192 strchinesesmall = onlysmall (intsmall );

193}

194 else

195 ...{

196 strzheng = "integer ";

197}

198 strall = strchineseint + strchinesesmall + strzheng;

199 return strall;

200}

201 public static void main (string ARGs []) throws ioexception

202 ...{

203 chinesemoney CM = new chinesemoney ();

204 double money;

205 string strmoney, strchinesemoney;

206 strmoney = "";

207 // read

208 system. Out. println ("input currency (rounding ):");

209 bufferedreader CIN = new bufferedreader (New inputstreamreader (system. In ));

210 strmoney = cin. Readline ();

211 money = double. parsedouble (strmoney );

212 // money = 12346.465; // double. parsedouble (strmoney );

213 strchinesemoney = cm. getchinesemoney (money );

214 system. Out. println (strchinesemoney );

215}

216}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.