Chinaunix首页 | 论坛 | 博客
  • 博客访问: 503393
  • 博文数量: 99
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 783
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-12 09:11
文章分类

全部博文(99)

文章存档

2023年(2)

2022年(1)

2020年(1)

2019年(1)

2018年(4)

2017年(16)

2016年(60)

2015年(1)

2013年(3)

2006年(10)

我的朋友

分类: Java

2022-09-05 12:27:06

      今天在网络上找JAVA版本的数字转人民币大写,找了几个版本,下载使用后都不怎么理想,或多或少存在点问题,另外一个原因就是出现了更大的金额兆(万亿),最后在前辈的基础上自己动手写了一个版本。


        网络上找的版本在处理小数点前面部分基本都是一个整体考虑,这次自己写的版本则采用4位一个分割的方式处理,测试后的结果和WPS的EXCEL进行了对比,测试案例中都保持了一致,草稿版本,有问题欢迎指正。


        测试过程中也发现了WPS的EXCEL对数字转人民币大写的最高限制,见图片黄色部分:






 
代码如下:


点击(此处)折叠或打开

  1. import java.math.BigDecimal;
  2. import java.text.DecimalFormat;
  3.  
  4. public class ToChinseMoney {
  5.  
  6.     private static String[] unit = {"", "万", "亿", "兆", "京"};
  7.     private static String[] beforeScale = {"仟", "佰", "拾", ""};
  8.     private static String[] numArray = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
  9.     private static String[] afterScale = {"角", "分"};
  10.  
  11.     public static String toUpperCaseZhCn(BigDecimal value) {
  12.         StringBuffer buf = new StringBuffer();
  13.         if (value.compareTo(new BigDecimal("0")) < 0)
  14.             buf.append("负");
  15.         value = value.abs();
  16.         String c[] = value.toString().split("\\.");//分割出来,.前面对应角分
  17.         splitByFour(c[0], buf);
  18.        
  19.         if (c.length > 1 && (c[1].toCharArray()[0] - 48 != 0 || c[1].toCharArray()[1] - 48 != 0)) {
  20.             char[] decimal = c[1].toCharArray();
  21.             if (decimal[0] - 48 == 0)
  22.                 buf.append(numArray[decimal[0] - 48]);
  23.             else
  24.                 buf.append(numArray[decimal[0] - 48]).append(afterScale[0]);
  25.             if (decimal.length > 1 && decimal[1] - 48 != 0)
  26.                 buf.append(numArray[decimal[1] - 48]).append(afterScale[1]);
  27.         }
  28.  
  29.         return buf.toString();
  30.     }
  31.  
  32.     public static void splitByFour(String strIntpart, StringBuffer buf) {
  33.           if(new BigDecimal("0").equals( new BigDecimal(strIntpart)))
  34.             {
  35.             buf.append("零");
  36.             return;
  37.             }
  38.  
  39.         DecimalFormat df4 = new DecimalFormat("####,####,####,####,####");
  40.         String formatMount = df4.format(new BigDecimal(strIntpart));
  41.         String[] splitList = formatMount.split(",");
  42.         if (splitList.length > 5) {
  43.             System.out.println("金额超限!");
  44. // throw new Exception("金额超限!");
  45.         }
  46.         for (int n = 0; n < splitList.length; n++) {
  47.             String onePart = splitList[n];
  48.             if (Integer.parseInt(onePart) != 0){
  49.                 RecursionChangeTo(onePart, buf, false);
  50.                 buf.append(unit[splitList.length - 1 - n]);
  51.             }
  52.         }
  53.  
  54.     }
  55.  
  56.     public static void RecursionChangeTo(String strIntpart, StringBuffer buf, boolean preIsZero) {
  57.         BigDecimal Intpart = new BigDecimal(strIntpart);
  58.         boolean iszero = false;
  59.  
  60.         String topone = "";
  61.         if (strIntpart.length() > 1)
  62.             topone = strIntpart.substring(0, 1);
  63.         else
  64.             topone = strIntpart;
  65.         int inttopone = Integer.parseInt(topone);
  66.  
  67.         if (inttopone != 0) {
  68.             if (preIsZero)//前面是0 ,这里不是0
  69.                 buf.append("零");
  70.             //金额部分
  71.             buf.append(numArray[inttopone]);
  72.             //单位部分
  73.             buf.append(beforeScale[beforeScale.length - strIntpart.length()]);
  74.         } else
  75.             iszero = true;
  76.  
  77.         if (strIntpart.length() > 1) {
  78.             String nextString = strIntpart.substring(1, strIntpart.length());
  79.             if (inttopone != 0)
  80.                 RecursionChangeTo(nextString, buf, false);
  81.             else
  82.                 RecursionChangeTo(nextString, buf, true);
  83.         }
  84.     }
  85.  
  86.     
  87.  
  88. }

阅读(250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~