java实现浮点数转人民币的小例子
程序员文章站
2023-11-27 09:49:46
复制代码 代码如下:import java.util.arraylist; import java.util.list; public class rmbconverter...
复制代码 代码如下:
import java.util.arraylist;
import java.util.list;
public class rmbconverter2 implements irmbconverter {
private static final string [] rmb_number ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private static final string [] rmb_attr = {"元","十","佰","仟","万","十","百","仟","亿","十","佰","仟","万","十","佰","仟"};
private static final string[] rmb_fen = {"分","角"};
@override
public string change2rmb(double counts) {
stringbuffer rsbuf = new stringbuffer();
if (counts == 0l) {
return "零";
}
if (counts < 0)
rsbuf.append("负");
string zheng = strconverse(string.valueof((long)counts));
string xiao = strconverse(string.valueof(math.round((counts - (long)counts)*100)));
char [] zhengarray = zheng.tochararray();
char [] xiaoarray = xiao.tochararray();
int zlength = zhengarray.length;
int xlength = xiaoarray.length;
list<string> zhenglist = new arraylist<string>();
for(int i =0; i< zlength; i++) {
int value = integer.parseint(""+zhengarray[i]);
if (value == 0 && (i != 4) && (i != 8) && i != 0) {
zhenglist.add(rmb_number[value]);
}else {
zhenglist.add(rmb_number[value]+
rmb_attr[i]);
}
}
for(int i = zhenglist.size()-1; i>=0; i--) {
rsbuf.append(zhenglist.get(i));
}
for(int i =0; i<xlength ;i++) {
int value = integer.parseint(""+xiaoarray[i]);
if (value > 0 ) {
rsbuf.append(rmb_number[value])
.append(rmb_fen[xlength-i-1]);
}
}
string rs = rsbuf.tostring();
rs = rs.replaceall("零+", "零");
rs = rs.replaceall("零元", "元");
rs = rs.replaceall("零万", "万");
rs = rs.replaceall("零亿", "亿");
rs = rs.replaceall("亿万", "亿");
return rs;
}
private string strconverse(string str) {
stringbuffer rs = new stringbuffer();
char [] strarray = str.tochararray();
for(int i = str.length()-1; i>=0; i--) {
rs.append(strarray[i]);
}
return rs.tostring();
}
public static void main(string args []) {
rmbconverter2 c2 = new rmbconverter2();
system.out.println(c2.change2rmb(2001004.10));
system.out.println(c2.change2rmb(2000000.10));
system.out.println(c2.change2rmb(0l));
system.out.println(c2.change2rmb(1121000010000000l));
}
}
上一篇: 香菜的功效与作用有哪些!