今天在超市,忽然想到可以写一段代码实现当收银员输入阿拉伯数字的人民币时,能够以规范的大写形式将其自动打印到发票上。
注:转换规范如下示例:
(1) 100000010100.09
人民币壹仟亿零壹万零壹佰元零角玖分
(2) 123456789.00
人民币壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整
(3) 1000000000.09
人民币拾亿元零角玖分
以下是我使用java实现该功能的代码:
import java.util.Scanner;
public class RMBSwitch {
public static void main(String[] args){
System.out.println("please input a double number");
Scanner scanner=new Scanner(System.in);//最多允许输入:左边12位,右边必须两位
String RMBArabic=scanner.next();
String RMBCN="人民币";
String[] totalInput=RMBArabic.split("\\.");
System.out.println("The result is");
System.out.println(RMBCN+leftToCN(totalInput[0])+rightToCN(totalInput[1]));
scanner.close();
}
//转换左边整数部分
public static String leftToCN(String left){
String leftCN="";
String[] ZHCN={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String[] unitCN={"","拾","佰","仟"};
if (left.length()<=4) {
for (int i = 0; i < left.length(); i++) {
leftCN=leftCN+ZHCN[Integer.valueOf(left.charAt(i))-48]+unitCN[left.length()-i-1];
}
leftCN=leftCN+"元";
}
if (left.length()<=8&&left.length()>4) {
for (int i = 0; i < left.length()-4; i++) {
leftCN=leftCN+ZHCN[Integer.valueOf(left.charAt(i))-48]+unitCN[left.length()-i-5];
}
leftCN=leftCN+"万";
for (int i = left.length()-4; i < left.length(); i++) {
leftCN=leftCN+ZHCN[Integer.valueOf(left.charAt(i))-48]+unitCN[left.length()-i-1];
}
leftCN=leftCN+"元";
}
if (left.length()<=13&&left.length()>8) {
for (int i = 0; i < left.length()-8; i++) {
leftCN=leftCN+ZHCN[Integer.valueOf(left.charAt(i))-48]+unitCN[left.length()-i-9];
}
leftCN=leftCN+"亿";
for (int i = left.length()-8; i < left.length()-4; i++) {
leftCN=leftCN+ZHCN[Integer.valueOf(left.charAt(i))-48]+unitCN[left.length()-i-5];
}
leftCN=leftCN+"万";
for (int i = left.length()-4; i < left.length(); i++) {
leftCN=leftCN+ZHCN[Integer.valueOf(left.charAt(i))-48]+unitCN[left.length()-i-1];
}
leftCN=leftCN+"元";
}
if (leftCN.charAt(0)=='壹'&&leftCN.charAt(1)=='拾') {
leftCN=leftCN.substring(1);
}
while(scanString(leftCN)==1){
for (int i = 0; i < leftCN.length()-1; i++) {
if (leftCN.charAt(i)=='零'&&(leftCN.charAt(i+1)=='亿'||leftCN.charAt(i+1)=='万')) {
leftCN=leftCN.substring(0, i)+leftCN.substring(i+1);
}
if (leftCN.charAt(i)=='零'&&(leftCN.charAt(i+1)=='拾'||leftCN.charAt(i+1)=='佰'||leftCN.charAt(i+1)=='仟'||leftCN.charAt(i+1)=='元'||leftCN.charAt(i+1)=='零')) {
leftCN=leftCN.substring(0, i+1)+leftCN.substring(i+2);
}
}
}
if (leftCN.charAt(leftCN.length()-1)=='零') {
leftCN=leftCN.substring(0,leftCN.length()-1)+"元";
}
if (leftCN.charAt(leftCN.indexOf('万')-1)=='亿') {
leftCN=leftCN.substring(0,leftCN.indexOf('万'))+leftCN.substring(leftCN.indexOf('万')+1);
}
return leftCN;
}
public static int scanString(String stringIn){
int flag=0;
for (int i = 0; i < stringIn.length()-1; i++) {
if (stringIn.charAt(i)=='零'&&(stringIn.charAt(i+1)=='万'||stringIn.charAt(i+1)=='亿')) {
flag=1;
break;
}
if (stringIn.charAt(i)=='零'&&(stringIn.charAt(i+1)=='拾'||stringIn.charAt(i+1)=='佰'||stringIn.charAt(i+1)=='仟'||stringIn.charAt(i+1)=='元'||stringIn.charAt(i+1)=='零')) {
flag=1;
break;
}
}
return flag;
}
//转换右边小数部分
public static String rightToCN(String right){
String[] ZHCN={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String rightCN="";
if (right.charAt(0)=='0'&&right.charAt(1)=='0') {
rightCN=rightCN+"整";
}else {
rightCN=rightCN+ZHCN[Integer.valueOf(right.charAt(0))-48]+"角"+ZHCN[Integer.valueOf(right.charAt(1))-48]+"分";
}
return rightCN;
}
}
我的实现思路为:先将其全部读出来,不考虑多个零或者单个零的情况,然后再处理读出来的字符串中的零,如果谁有更好的方法可以在评论区留言,谢谢!
标记为原创的博文均为本人辛苦码字所得,谢绝抄袭,转载请注明出处,新浪微博私信艾特:物联网工程_Niegang。