Examples of tools that Huawei's tens of thousands of employees want to know about the amount of money converted from 10,000 yuan to 10,000 yuan in Java

This article mainly introduces an example of the tool class that converts the amount of yuan to 10,000 yuan in java, which has a good reference value, and I hope it will be helpful to everyone. Let's follow the editor to take a look

I won't talk too much nonsense, everyone should look at the code directly~

public static void main(String[] args) {
    
    
  // 具体的金额(单位元)
  String value = "88000067898";
  BigDecimal bigDecimal = new BigDecimal(value);
  // 转换为万元(除以10000)
  BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
  // 保留两位小数
  DecimalFormat formater = new DecimalFormat("0");
  // 四舍五入
  formater.setRoundingMode(RoundingMode.HALF_UP); 
  // 格式化完成之后得出结果
  String formatNum = formater.format(decimal);
  System.out.println(formatNum);
 }
 /**元转万元且四舍五入取整*/
 public static String getNumberWan(String value) {
    
    
  BigDecimal bigDecimal = new BigDecimal(value);
  // 转换为万元(除以10000)
  BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
  // 保留两位小数
  DecimalFormat formater = new DecimalFormat("0");
  // 四舍五入
  formater.setRoundingMode(RoundingMode.HALF_UP); 
  // 格式化完成之后得出结果
  String rs = formater.format(decimal);
  return rs;
 }
  
 /**元转万元且保留两位小数并四舍五入*/
 public static String getNumberWanTwo(String value) {
    
    
  BigDecimal bigDecimal = new BigDecimal(value);
  // 转换为万元(除以10000)
  BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
  // 保留两位小数
  DecimalFormat formater = new DecimalFormat("0");
  // 四舍五入
  formater.setRoundingMode(RoundingMode.HALF_UP); 
  // 格式化完成之后得出结果
  String rs = formater.format(decimal);
  return rs;
 }

Supplement: Use java to convert the amount from a number to a digital amount in Chinese

Write an amount converted from a number to a number in Chinese

E.g:

123123.12 converted to one ten thousand two thousand three thousand one hundred two ten three yuan one dime two cents

Since the code is copied directly from the development process, some of it can be ignored. The newcomer has just worked and the writing is not good. Please correct me.

import java.util.Scanner;
public class Test {
    
    
  //数字单位
  private static final String[] NUMBERS_UNITS= {
    
    "拾","佰","仟","万"};
  public static void main(String[] args) {
    
    
    Test st=new Test();
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入金额:");
    //获取前端传递过来的金额数字
    String money=scanner.nextLine();
    //1.判断是否为null
    st.isNull(money);
    //2.判断是否有非法字符
    st.isIllegal(money);
    //3.判断是否超过限额
    String[] array=st.isBeyondMax(money);
    //4.整数位字符转换
    StringBuffer chineseInt=st.convertInt(array[0]);
    //5.判断有无小数位
    if(array.length==2) {
    
    
      //6.有则进行小数位字符转换
      StringBuffer chineseDec=st.convertDec(array[1]);
      //7.拼接整数和小数位
      chineseInt=chineseInt.append(chineseDec);
    }
    //8.将转为大写的金额设置回总线
    System.out.println(chineseInt);
  }
  /**
   * 将小数位的金额数字转化为中文大写
   * @param string 金额数字
   * @return 转为中文的金额数字
   */
  private StringBuffer convertDec(String string) {
    
    
    StringBuffer str=convert(string);
    switch(str.length()) {
    
    
      case 1:
        str.append("角");
        break;
      case 2:
        str.append("分");
        str.insert(1, "角");
        break;
    }
    return str;
  }
  /**
   * 将整数位的金额数字对应转化为中文大写
   * @param string 金额数字
   * @return 转为中文的金额数字
   */
  private StringBuffer convertInt(String string) {
    
    
    StringBuffer str=convert(string);
    int length=str.length()-1;
    for (int i = 0,j=str.length()-1; i < length; i++,j--) {
    
    
      int v=i%4;
      System.out.println("i:"+i+" j:"+j+" v:"+v+" "+str.toString());
      str.insert(j, NUMBERS_UNITS[v]);
    }
    str.append("元");
    return str;
  }
  /**
   * 循环整个字符串,替换字符
   * @param string 要替换的字符串
   * @return 替换好的字符
   */
  private StringBuffer convert(String string) {
    
    
    StringBuffer str=new StringBuffer(string);
    for (int i = 0; i < str.length(); i++) {
    
    
      str.replace(i, i+1,replaceCharacter(str.substring(i, i+1)));
    }
    return str;
  }
  /**
   * 将给定的数字字符替换为对应的中文数字字符
   * @param string 数字字符
   * @return 中文数字字符
   */
  private String replaceCharacter(String string) {
    
    
    switch(string) {
    
    
      case "0":
        string="零";
        break;
      case "1":
        string="壹";
        break;
      case "2":
        string="贰";
        break;
      case "3":
        string="叁";
        break;
      case "4":
        string="肆";
        break;
      case "5":
        string="伍";
        break;
      case "6":
        string="陆";
        break;
      case "7":
        string="柒";
        break;
      case "8":
        string="捌";
        break;
      case "9":
        string="玖";
        break;
    }
    return string;
  }
  /**
   * 检测传入金额字符是否为空
   * @param money 金额字符
   */
  private void isNull(String money) {
    
    
    if(money == null) {
    
    
      //抛出异常
    }
  }
  /**
   * 检测传入金额字符是否符合条件
   * @param money 金额字符
   */
  private void isIllegal(String money) {
    
    
    if(!money.matches("\\d+.?\\d*")) {
    
    
      //抛出异常
    }
  }
  /**
   * 检测传入金额字符是否超出最大值
   * @param money 金额字符
   * @return 返回拆分的金额数字
   */
  private String[] isBeyondMax(String money) {
    
    
    String[] array=money.split("\\.");
    //整数位不能超过9位,小数位不能超过2为
    if(array[0].length()>9 || (array.length>1 &&array[1].length()>2)) {
    
    
      //抛出异常
    }
    return array;
  }
}

The above is personal experience, I hope to give you a reference, and I hope you can support the editor. If there are mistakes or not fully considered, please feel free to enlighten me.

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115253497