j**a:人民币大小写转换:比如比输入123456,要求输出:拾贰万叁仟肆佰伍拾柒,请不要用数组方法
super琳zl_
2024-11-15 18:42:01
最佳回答
public class moneysmalltobig {转自csdn的东东/** * 人民币大小写转换 */ //别说这里用数组,这里定义大写的是必须的public static string[] number = new string[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };public static string[] units = new string[]{"元","拾","佰","仟"}; public static void change(string smoney){stringbuffer **=new stringbuffer();//小数点的位置int index=smoney.indexof(".");//biggerstr用于存放存有数字和单位的字符stringbuffer biggerstr=new stringbuffer();//处理小数部分biggerstr.append("分");biggerstr.append(number[integer.parseint(smoney.charat(index+2)+"")]);biggerstr.append("角");biggerstr.append(number[integer.parseint(smoney.charat(index+1)+"")]);//处理整数部分for(int i=0;i<index;i++){**.append(number[integer.parseint(smoney.charat(i)+"")]);}**=**.reverse();string insertstr=**.tostring();for(int i=0;i<insertstr.length();i++){//插入单位if(i==0){biggerstr.append("元");}else if((i+4)%4==0 && i!=8){biggerstr.append("万");}else if(i%8==0){biggerstr.append("亿");}elsebiggerstr.append(units[i%4]);//插入数字biggerstr.append(insertstr.charat(i));}//instead_zero字符串用于存放清除零之后的string instead_zero=biggerstr.reverse().tostring();instead_zero=instead_zero.replaceall("零分", "零");instead_zero=instead_zero.replaceall("零角", "零");instead_zero=instead_zero.replaceall("零佰", "零");instead_zero=instead_zero.replaceall("零仟", "零");instead_zero=instead_zero.replaceall("零拾", "零");//把相连的零去掉instead_zero=instead_zero.replaceall("[零]+","零");instead_zero=instead_zero.replaceall("零元", "元");instead_zero=instead_zero.replaceall("零万", "万");instead_zero=instead_zero.replaceall("零亿", "亿");//把字符串中的最后一个零去掉if(instead_zero.charat(instead_zero.length()-1)=='零'){instead_zero=instead_zero.substring(0, instead_zero.length()-1);}system.out.println(instead_zero);}public static void main(string[] args) {string smoney="1020089090.89";change(smoney);}} 20210311