RMB转换大写
RMB转换大写
裸奔的八戒 发表于2年前
RMB转换大写
  • 发表于 2年前
  • 阅读 64
  • 收藏 2
  • 点赞 1
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

public class RMB{


private final static String[] STR_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

private final static String[] STR_UNIT = { "", "拾", "佰", "仟", "万", "拾","佰", "仟", "亿", "拾", "佰", "仟" };// 整数单位

private final static String[] STR_UNIT2 = { "角", "分", "厘" };// 小数单位

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan= new Scanner(System.in);

System.out.println("Please input a number: ");

String convert=convert(scan.nextDouble());

System.out.println(convert);

}

public static boolean IsNumber(String str){

char[] c=str.toCharArray();

for(int i=0;i<c.length;i++){

if(Character.isDigit(c[i])){

}else{

return false;

}

}

return true;

}

public static String convert(double d){

DecimalFormat df=new DecimalFormat("#0.###");

String strNum=df.format(d);

if(strNum.indexOf(".")!=-1){

String num=strNum.substring(0, strNum.indexOf("."));

if(num.length()>12){

System.out.println("number is too big,can not be convert!");

return "";

}

}

String point="";

if(strNum.indexOf(".")!=-1){

point="元";

}else{

point="元整";

}

String result=getInteger(strNum)+point+getDecimal(strNum);

if(result.startsWith("元")){

result=result.substring(1, result.length());

}

return result;

}

public static String getInteger(String num){

if(num.indexOf(".")!=-1){

num=num.substring(0,num.indexOf("."));

}

num=new StringBuffer(num).reverse().toString();

StringBuffer temp=new StringBuffer();

for(int i=0;i<num.length();i++){

temp.append(STR_UNIT[i]);

temp.append(STR_NUMBER[num.charAt(i)-48]);

}

num=temp.reverse().toString();

num=numReplace(num,"零拾","零");

num=numReplace(num,"零佰","零");

num=numReplace(num,"零仟","零");

num=numReplace(num,"零万","万");

num=numReplace(num,"零亿","亿");

num=numReplace(num,"零零","零");

num=numReplace(num,"亿万","亿");

if(num.lastIndexOf("零")==num.length()-1){

num=num.substring(0,num.length()-1);

}

return num;

}

public static String getDecimal(String num){

if (num.indexOf(".") == -1){

return "";

}

num = num.substring(num.indexOf(".") + 1);

num = new StringBuffer(num).reverse().toString();

StringBuffer temp = new StringBuffer();

for (int i = 0; i < num.length(); i++){

temp.append(STR_NUMBER[num.charAt(i) - 48]);

temp.append(STR_UNIT2[i]);

}

num = temp.toString();

num = numReplace(num, "零角", "零");

num = numReplace(num, "零分", "零");

num = numReplace(num, "零厘", "零");

num = numReplace(num, "零零", "零");

if (num.lastIndexOf("零") == num.length() - 1){

num = num.substring(0, num.length() - 1);

}

return num;

}

public static String numReplace(String num, String oldStr, String newStr){

while(true){

if (num.indexOf(oldStr) == -1){

break;

}

num = num.replaceAll(oldStr, newStr);

}

return num;

}


}


共有 人打赏支持
粉丝 3
博文 44
码字总数 6845
×
裸奔的八戒
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: