将一个小于10亿的String类型数字转换为人民币大写形式

package commons;   

/@@@   

@ 浮点数字转换到人民币大写形式程序  

@ 功能: 将一个小于10亿的String类型数字转换为人民币大写形式  

@ 例如 123456789.12 壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元壹角贰分  

@ 编写人:LEOPENG   

@ 完成时间:2008-11-30   

@ 版本:1.0.0   

@/   

public class Float2RMBconverter {   

            public static void main(String[] args) {   

                       Float2RMBconverter c = new Float2RMBconverter();

                 String s = c.convert("020081130.82");   

                       System.out.println(s);   

            }   

            public String convert(String number) {

                  if (!number.matches("[0-9]{1,9}\\.[0-9]{1,2}")) {

                            return "Invalidate number format!";   

}   String[] partion = number.split("\\.");   String lPart = partion[0];   String rPart = partion[1];   while (lPart.length() != 9) {   lPart = "0" + lPart;   }   StringBuffer rsLeft = new StringBuffer();   StringBuffer rsRight = new StringBuffer();   String result = new String();   int len = lPart.length();   if (len == 9) {   rsLeft.append(this.processYi(lPart.subSequence(0, 1).toString()));   rsLeft.append(this.processWan(lPart.subSequence(1, 5).toString()));   rsLeft.append(this.processQian(lPart.subSequence(5, 9).toString()));   rsRight.append(this.processJiaoFen(rPart));   result += rsLeft;   result += rsRight;   if (result.contains("零零零")) {   result = result.replace("零零零", "零");   }   if (result.contains("零零")) {   result = result.replace("零零", "零");   }   if (result.startsWith("零")) {   result = result.substring(1);   }   } else {   return number;   }   return result;   }   private String processYi(String yi) {   if (yi.equals("0")) {   return "";   }   return this.replaceAllNumber(yi) + "亿";   }   private String processWan(String wan) {   String w1 = wan.subSequence(0, 1).toString();   String w2 = wan.subSequence(1, 2).toString();   String w3 = wan.subSequence(2, 3).toString();   String w4 = wan.subSequence(3, 4).toString();   if (w1.equals("0")) {   } else {   w1 = this.replaceAllNumber(w1) + "仟";   }   if (w2.equals("0")) {   } else {   w2 = this.replaceAllNumber(w2) + "佰";   }   if (w3.equals("0")) {   } else {   w3 = this.replaceAllNumber(w3) + "拾";   }   if (w4.equals("0")) {   w4 = "0万";   } else {   w4 = this.replaceAllNumber(w4) + "万";   }   return replaceWanZero(w1 + w2 + w3 + w4);   }   private String processQian(String qian) {   String q1 = qian.subSequence(0, 1).toString();   String q2 = qian.subSequence(1, 2).toString();   String q3 = qian.subSequence(2, 3).toString();   String q4 = qian.subSequence(3, 4).toString();   if (q1.equals("0")) {   } else {   q1 = this.replaceAllNumber(q1) + "仟";   }   if (q2.equals("0")) {   } else {   q2 = this.replaceAllNumber(q2) + "佰";   }   if (q3.equals("0")) {   } else {   q3 = this.replaceAllNumber(q3) + "拾";   }   if (q4.equals("0")) {   q4 = "0元";   } else {   q4 = this.replaceAllNumber(q4) + "元";   }   return replaceQianZero(q1 + q2 + q3 + q4);   }   private String processJiaoFen(String n) {   String jiao = n.subSequence(0, 1).toString();   String fen = null;   if (n.length() > 1)   fen = n.subSequence(1, 2).toString();   if (jiao.equals("0")) {   jiao = "零";   } else {   jiao = this.replaceAllNumber(jiao) + "角";   }   if (fen == null)   fen = new String("0");   if (fen.equals("0")) {   fen = new String("零");   } else {   fen = this.replaceAllNumber(fen) + "分";   }   if (jiao.equals("零") && fen.equals("零")) {   return "整";   }   String rs = jiao + fen;   while (rs.endsWith("零")) {   rs = rs.subSequence(0, rs.length() - 1).toString();   }   return rs;   }   private String replaceAllNumber(String n) {   String nr;   nr = n.replace("1", "壹");   nr = nr.replace("2", "贰");   nr = nr.replace("3", "叁");   nr = nr.replace("4", "肆");   nr = nr.replace("5", "伍");   nr = nr.replace("6", "陆");   nr = nr.replace("7", "柒");   nr = nr.replace("8", "捌");   nr = nr.replace("9", "玖");   nr = nr.replace("0", "零");   return nr;   }   private String replaceWanZero(String n) {   if (n.contains("0000")) {   n = "零";   return n;   }   if (n.length() > 0) {   n = n.subSequence(0, n.length() - 1).toString();   while (n.endsWith("0")) {   n = n.subSequence(0, n.length() - 1).toString();   }   n = n + "万";   }   if (n.contains("000")) {   n = n.replace("000", "零");   }   if (n.contains("00")) {   n = n.replace("00", "零");   }   if (n.contains("0")) {   n = n.replace("0", "零");   }   return n;   }   private String replaceQianZero(String n) {   if (n.contains("0000")) {   n = "零";   return n;   }   if (n.length() > 0) {   n = n.subSequence(0, n.length() - 1).toString();   while (n.endsWith("0")) {   n = n.subSequence(0, n.length() - 1).toString();   }   n = n + "元";   }   if (n.contains("000")) {   n = n.replace("000", "零");   }   if (n.contains("00")) {   n = n.replace("00", "零");   }   if (n.contains("0")) {   n = n.replace("0", "零");   }   return n;   }   }// end Float2RMBconverter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值