金额转换,阿拉伯数字转换成中国传统形式。

来源:互联网 发布:淘宝开店的押金怎么退 编辑:程序博客网 时间:2024/05/17 06:23

10、金额转换,阿拉伯数字转换成中国传统形式。 
   例如:101000001010   转换为   壹仟零壹拾億零壹仟零壹拾圆整。
此题的解题主要要抓住几个关键点,
1.判断录入的数字是否合法
首先录入的数据要验证它是否是合法的数字,这个验证的方法我第一想到的是使用正则表达式来处理,但是我写了很多正则都没能满足要求,就暂时用if else代替了。

2.根据要求的精度舍去多的小数位数的数字,并且去掉末尾的0和多余的小数点,需要用到while语句循环处理直到满足要求为止

3.数字转换 单位转换

·在数字的转换的过程中药分整数和负数两种情况,负数的起始index返回的是符号-不是数字,所以巧妙的设计一个判断数字正负的if语句把后面要遍历循环的起始参数变量初始化为0或者1。

·添加单位和数字也是有讲究的,使用StringTokenizer类按照符号"."将字符串分成小数部分和整数部分分别来处理。小数部分的单位转换和数字转换比较容易,就是依次给非0的数字加上中文的数字和单位即可。整数部分的添加就要注意了是从低位向高位依次添加单位,然后再插入数字,在这个逆向的遍历操作中,用if语句判断起始字符串是否为零的情况来控制重复加零的操作。
·最后提醒自己完善一些细节:
 -先判断出数值就为零的情况直接返回
 -整数部分的处理,StringBuilder初始化字符串是币种单位“圆”
 -最后给整数部分和小数部分都添加完单位和数字后,判断是否为负数给字符串加前缀“负”,判断有没有小数部分给字符串加后缀 “整”。

4.以上转换得到的字符串中存在零十、零百、零千、零万、零億和以圆、负圆开头的字符的情况,分别过滤掉前面的零?和 以圆、负圆开头的字符中的“圆”字符

 

主要代码如下:

package lqq.heima.matriculation.subject10;

import java.util.HashMap;

public class Units {

 public static final String EMPTY = "";
 public static final String ZERO = "零";
 public static final String ONE = "壹";
 public static final String TWO = "贰";
 public static final String THREE = "叁";
 public static final String FOUR = "肆";
 public static final String FIVE = "伍";
 public static final String SIX = "陆";
 public static final String SEVEN = "柒";
 public static final String EIGHT = "捌";
 public static final String NINE = "玖";
 public static final String TEN = "拾";
 public static final String HUNDRED = "佰";
 public static final String THOUSAND = "仟";
 public static final String TEN_THOUSAND = "万";
 public static final String HUNDRED_MILLION = "億";
 public static final String YUAN = "圆";
 public static final String JIAO = "角";
 public static final String FEN = "分";
 public static final String DOT = ".";
 public static final String ZHENG ="整";
 public static final String MINUS = "-";
 public static final String MINUS_TO_CHINESE = "负";
 
 public static final String PATTERN ="0123456789.";
 public static final int MAXIMUM_FRACTION_DIGITS = 2;

 private static Units units = null;
 private HashMap<String, String> chineseNumberMap = new HashMap<String, String>();
 private HashMap<String, String> chineseMoneyPattern = new HashMap<String, String>();

 private Units() {

  chineseNumberMap.put("0", "");
  chineseNumberMap.put("1", ONE);
  chineseNumberMap.put("2", TWO);
  chineseNumberMap.put("3", THREE);
  chineseNumberMap.put("4", FOUR);
  chineseNumberMap.put("5", FIVE);
  chineseNumberMap.put("6", SIX);
  chineseNumberMap.put("7", SEVEN);
  chineseNumberMap.put("8", EIGHT);
  chineseNumberMap.put("9", NINE);
  chineseNumberMap.put(DOT, DOT);

  chineseMoneyPattern.put("1", TEN);
  chineseMoneyPattern.put("2", HUNDRED);
  chineseMoneyPattern.put("3", THOUSAND);
  chineseMoneyPattern.put("4", TEN_THOUSAND);
  chineseMoneyPattern.put("5", HUNDRED_MILLION);
 }

 public static Units getInstance() {
  if (units == null)
   units = new Units();
  return units;
 }

 public HashMap<String, String> getChineseNumberMap() {
  return chineseNumberMap;
 }

 public HashMap<String, String> getChineseMoneyPattern() {
  return chineseMoneyPattern;
 }

 


   
}

 

package lqq.heima.matriculation.subject10;

import java.util.StringTokenizer;

@SuppressWarnings("static-access")
public class SimpleMoneyFormat {

 private Units units;
 private String prefix;
 private String suffix;
 private String symbol = "+";

 public SimpleMoneyFormat() {
  this.units = Units.getInstance();
 }

 public String format(String num) {

  num = num.trim();
  String result = "";
  if (isPattern(num)) {
   if (num.startsWith(Units.MINUS)) {
    this.symbol = Units.MINUS;
    num = num.substring(1);
   }
   spit(round(num));
   result = addUnitsToChineseMoneyString();
  } else {
   System.out.println("非法数字。");
  }

  return filter(result);
 }

 private boolean isPattern(String num) {

  // return num.matches("^//d+$");
  boolean flag = true;
  int i; // 正负数
  int count = 0; // 计算小数点个数

  // 不为空
  if (num != null && !num.equals("")) {
   // 正负数
   if (num.startsWith("-"))
    i = 1;
   else
    i = 0;

   if (String.valueOf(num.charAt(i)).equals("0")) {
    int temp = i;

    if (!String.valueOf(num.charAt(++temp)).equals(Units.DOT)) {
     flag = false;
     return flag;
    }
   }

   for (; i < num.length() - 1; i++) {
    if (units.PATTERN.indexOf(num.charAt(i)) == -1) {
     flag = false;
     return flag;

    }

    if ((num.charAt(i) + "").equals("."))
     count++;
   }

   // 小数点后没数据
   if (num.endsWith("."))
    flag = false;

   // 不止一個小數點
   if (count > 1)
    flag = false;
  }

  return flag;

 }


 private String filter(String result) {
  String temp = result.replace(units.ZERO + units.TEN, "").replace(
    units.ZERO + units.HUNDRED, "").replace(
    units.ZERO + units.THOUSAND, "").replace(
    units.ZERO + units.TEN_THOUSAND, "").replace(
    units.ZERO + units.HUNDRED_MILLION, "");
  if (temp.startsWith(Units.YUAN) || temp.startsWith("负" + Units.YUAN)) {
   temp = temp.replace(Units.YUAN, "");
  }
  return temp;
 }

 private String round(String num) {
  String result = "";
  if (num.indexOf(units.DOT) != -1) {
   int length = num.length() - (num.indexOf(Units.DOT) + 1);

   if (length > Units.MAXIMUM_FRACTION_DIGITS) {

    result = num
      .substring(
        0,
        (num.length() - (length - Units.MAXIMUM_FRACTION_DIGITS)));

   } else {
    result = num;
   }
   while (result.endsWith("0") || result.endsWith(Units.DOT)) {
    result = result.substring(0, result.length() - 1);
   }

  } else {
   result = num;
  }
  return result;
 }

 /**
  * @function 整数部分、小数部分初始化
  */
 private void spit(String num) {
  StringTokenizer st = new StringTokenizer(num, Units.DOT);

  if (st.countTokens() == 1)
   prefix = st.nextToken();
  else if (st.countTokens() == 2) {
   prefix = st.nextToken();
   suffix = st.nextToken();
  }
 }

 /**
  *
  * @param moneyStr
  * @return
  */
 private String addUnitsToChineseMoneyString() {
  String result = Units.YUAN;

  int count = 0;

  // 整数部分转换
  for (int i = prefix.length() - 1; i >= 0; i--) {
   count++;

   int j = Integer.parseInt(prefix.charAt(i) + ""); // 当前数字

   if (j == 0) {

    if (!result.startsWith(units.ZERO) && count > 1) {

     result = units.ZERO + result;
    }

    // 万位、亿位上为零,需要添加单位
    if ((count - 1) > 0 && (count - 1) % 4 == 0) {
     if ((count - 1) % 8 == 0)
      result = units.HUNDRED_MILLION + result;
     else
      result = units.TEN_THOUSAND + result;
    }
   } else {
    // 添加计量单位
    if (count % 2 == 0 && count % 4 != 0)
     result = units.TEN + result;
    else if ((count + 1) % 4 == 0)
     result = units.HUNDRED + result;
    else if (count % 4 == 0)
     result = units.THOUSAND + result;
    else if ((count - 1) > 0 && (count - 1) % 4 == 0
      && (count - 1) % 8 != 0)
     result = units.TEN_THOUSAND + result;
    else if ((count - 1) > 0 && (count - 1) % 8 == 0)
     result = units.HUNDRED_MILLION + result;
   }

   // 数字转大写
   result = units.getChineseNumberMap().get(String.valueOf(j))
     + result;
  }

  // 判断是否存在小数部分
  if (null != suffix) {
   // 小数部分转换

   for (int i = 0; i < suffix.length(); i++) {
    int k = Integer.parseInt(suffix.charAt(i) + "");

    if (k != 0) {
     result += units.getChineseNumberMap()
       .get(String.valueOf(k));

     if (i == 0)
      result += units.JIAO;
     else if (i == 1)
      result += units.FEN;

    }
   }
  } else {

   result = result + Units.ZHENG;
  }

  // 正负数
  if (this.symbol.equals(Units.MINUS))
   result = "负" + result;

  
  return result;
 }
}

package lqq.heima.matriculation.subject10;

public class MainClass {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   SimpleMoneyFormat1 smf = new SimpleMoneyFormat1();
   String num = "101000001010";
   System.out.println(num);
   System.out.println(smf.format(num));

 }

}

原创粉丝点击