欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java金额数字转中文工具类详解

程序员文章站 2023-12-12 16:32:04
本文实例为大家分享了java金额数字转中文工具类的具体代码,供大家参考,具体内容如下 java金额数字转中文工具类convertnum.java package...

本文实例为大家分享了java金额数字转中文工具类的具体代码,供大家参考,具体内容如下

java金额数字转中文工具类convertnum.java

package light.mvc.utils;
 
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
 
/**
 * 金额数字转中文工具类
 * 
 * @author ardo
 * 
 */
public class convertnum {
 
 /**
 * 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
 * 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
 */
 public static string[] chinesenum = new string[] { "零", "壹", "贰", "叁", "肆",
  "伍", "陆", "柒", "捌", "玖" };
 
 public static string numtochinese(double num) {
 if (num > 99999999999999.99 || num < -99999999999999.99)
  throw new illegalargumentexception(
   "参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)!");
 boolean negative = false;// 正负标号
 if (num < 0) {
  negative = true;
  num = num * (-1);
 }
 long temp = math.round(num * 100);
 int numfen = (int) (temp % 10);// 分
 temp = temp / 10;
 int numjiao = (int) (temp % 10);// 角
 temp = temp / 10;
 // 此时temp只包含整数部分
 int[] parts = new int[20];// 将金额整数部分分为在0-9999之间数的各个部分
 int numparts = 0;// 记录把原来金额整数部分分割为几个部分
 for (int i = 0;; i++) {
  if (temp == 0)
  break;
  int part = (int) (temp % 10000);
  parts[i] = part;
  temp = temp / 10000;
  numparts++;
 }
 boolean beforewaniszero = true;// 标志位,记录万的下一级是否为0
 string chinesestr = "";
 for (int i = 0; i < numparts; i++) {
  string partchinese = partconvert(parts[i]);
  if (i % 2 == 0) {
  if ("".equals(partchinese))
   beforewaniszero = true;
  else
   beforewaniszero = false;
  }
  if (i != 0) {
  if (i % 2 == 0)// 亿的部分
   chinesestr = "亿" + chinesestr;
  else {
   if ("".equals(partchinese) && !beforewaniszero)// 如果“万”对应的
         // part 为
         // 0,而“万”下面一级不为
         // 0,则不加“万”,而加“零”
   chinesestr = "零" + chinesestr;
   else {
   if (parts[i - 1] < 1000 && parts[i - 1] > 0)// 如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
    chinesestr = "零" + chinesestr;
   chinesestr = "万" + chinesestr;
   }
  }
  }
  chinesestr = partchinese + chinesestr;
 }
 if ("".equals(chinesestr))// 整数部分为0,则表示为零元
  chinesestr = chinesenum[0];
 else if (negative)// 整数部分部位0,但是为负数
  chinesestr = "负" + chinesestr;
 chinesestr = chinesestr + "元";
 if (numfen == 0 && numjiao == 0) {
  chinesestr = chinesestr + "整";
 } else if (numfen == 0) {// 0分
  chinesestr = chinesestr + chinesenum[numjiao] + "角";
 } else {
  if (numjiao == 0)
  chinesestr = chinesestr + "零" + chinesenum[numfen] + "分";
  else
  chinesestr = chinesestr + chinesenum[numjiao] + "角"
   + chinesenum[numfen] + "分";
 }
 return chinesestr;
 }
 
 // 转换拆分后的每个部分,0-9999之间
 public static string partconvert(int partnum) {
 if (partnum < 0 || partnum > 10000) {
  throw new illegalargumentexception("参数必须是大于等于0或小于10000的整数");
 }
 string[] units = new string[] { "", "拾", "佰", "仟" };
 int temp = partnum;
 string partresult = new integer(partnum).tostring();
 int partresultlength = partresult.length();
 boolean lastiszero = true;// 记录上一位是否为0
 string chinesestr = "";
 for (int i = 0; i < partresultlength; i++) {
  if (temp == 0)// 高位无数字
  break;
  int digit = temp % 10;
  if (digit == 0) {
  if (!lastiszero)// 如果前一个数字不是0则在当前汉字串前加零
   chinesestr = "零" + chinesestr;
  lastiszero = true;
  } else {
  chinesestr = chinesenum[digit] + units[i] + chinesestr;
  lastiszero = false;
  }
  temp = temp / 10;
 }
 return chinesestr;
 }
 
 public static void main(string args[]) {
 double num = 0;
 system.out.println("请输入金额数字,-1退出");
 try {
  bufferedreader br = new bufferedreader(new inputstreamreader(
   system.in));
  num = double.parsedouble(br.readline());
 } catch (ioexception e) {
  system.out.println(e.tostring());
 }
 while (num != -1) {
  system.out.println(num + numtochinese(num));
  try {
  bufferedreader br = new bufferedreader(new inputstreamreader(
   system.in));
  num = double.parsedouble(br.readline());
  } catch (ioexception e) {
  system.out.println(e.tostring());
  }
 }
 system.out.println("其他测试:");
 system.out.println("100120: " + numtochinese(100120));
 system.out.println("25000000000005.999: "
  + numtochinese(25000000000005.999));
 system.out.println("45689263.626: " + numtochinese(45689263.626));
 system.out.println("0.69457: " + numtochinese(0.69457));
 system.out.println("253.0: " + numtochinese(253.0));
 system.out.println("0: " + numtochinese(0));
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: