Example of Amount Yuan to Ten Thousand Yuan Tool Class in java

  • 2021-08-21 20:35:17
  • OfStack

I won't talk too much, let's just look at the code ~


public static void main(String[] args) {
  //  Specific amount (unit yuan) 
  String value = "88000067898";
  BigDecimal bigDecimal = new BigDecimal(value);
  //  Converted to ten thousand yuan (divided by 10000 ) 
  BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
  //  Keep two decimal places 
  DecimalFormat formater = new DecimalFormat("0");
  // 4 Shed 5 Into 
  formater.setRoundingMode(RoundingMode.HALF_UP); 
  //  The result is obtained after formatting is completed 
  String formatNum = formater.format(decimal);
  System.out.println(formatNum);
 }
 /** Yuan to 10,000 yuan and 4 Shed 5 Input rounding */
 public static String getNumberWan(String value) {
  BigDecimal bigDecimal = new BigDecimal(value);
  //  Converted to ten thousand yuan (divided by 10000 ) 
  BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
  //  Keep two decimal places 
  DecimalFormat formater = new DecimalFormat("0");
  // 4 Shed 5 Into 
  formater.setRoundingMode(RoundingMode.HALF_UP); 
  //  The result is obtained after formatting is completed 
  String rs = formater.format(decimal);
  return rs;
 }
 
 /** Yuan to 10,000 yuan and keep two decimal places and 4 Shed 5 Into */
 public static String getNumberWanTwo(String value) {
  BigDecimal bigDecimal = new BigDecimal(value);
  //  Converted to ten thousand yuan (divided by 10000 ) 
  BigDecimal decimal = bigDecimal.divide(new BigDecimal("10000"));
  //  Keep two decimal places 
  DecimalFormat formater = new DecimalFormat("0");
  // 4 Shed 5 Into 
  formater.setRoundingMode(RoundingMode.HALF_UP); 
  //  The result is obtained after formatting is completed 
  String rs = formater.format(decimal);
  return rs;
 }

Supplement: Using java to convert the amount from digital to Chinese digital amount

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

For example:

123123.12 converted to one hundred and twenty-three thousand one hundred and twenty three yuan and twelve cents

Because is from the development process of the code directly copy to come over, there is 1 part can be ignored, new people just work, write bad, please correct more


import java.util.Scanner;
public class Test {
  // Digital unit 
  private static final String[] NUMBERS_UNITS= {" Pick up "," Bai "," Thousands "," Ten thousand "};
  public static void main(String[] args) {
    Test st=new Test();
    Scanner scanner=new Scanner(System.in);
    System.out.println(" Please enter an amount :");
    // Get the amount number passed from the front end 
    String money=scanner.nextLine();
    //1. Determine whether it is null
    st.isNull(money);
    //2. Determine whether there are illegal characters 
    st.isIllegal(money);
    //3. Determine whether the limit is exceeded 
    String[] array=st.isBeyondMax(money);
    //4. Integer bit character conversion 
    StringBuffer chineseInt=st.convertInt(array[0]);
    //5. Determine whether there are decimal places 
    if(array.length==2) {
      //6. If yes, decimal character conversion is performed 
      StringBuffer chineseDec=st.convertDec(array[1]);
      //7. Splicing integers and decimal places 
      chineseInt=chineseInt.append(chineseDec);
    }
    //8. Set the capitalized amount back to the bus 
    System.out.println(chineseInt);
  }
  /**
   *  Convert decimal amount figures to Chinese capitals 
   * @param string  Amount figure 
   * @return  Amount converted into Chinese 
   */
  private StringBuffer convertDec(String string) {
    StringBuffer str=convert(string);
    switch(str.length()) {
      case 1:
        str.append(" Angle ");
        break;
      case 2:
        str.append(" Points ");
        str.insert(1, " Angle ");
        break;
    }
    return str;
  }
  /**
   *  Convert the amount figures of integer bits into Chinese capitals 
   * @param string  Amount figure 
   * @return  Amount converted into Chinese 
   */
  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(" Yuan ");
    return str;
  }
  /**
   *  Loop the whole string and replace the characters 
   * @param string  String to replace 
   * @return  Replaced characters 
   */
  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;
  }
  /**
   *  Replace the given numeric character with the corresponding Chinese numeric character 
   * @param string  Numeric character 
   * @return  Chinese numeric characters 
   */
  private String replaceCharacter(String string) {
    switch(string) {
      case "0":
        string=" Zero ";
        break;
      case "1":
        string=" One ";
        break;
      case "2":
        string=" 2 ";
        break;
      case "3":
        string=" 3 ";
        break;
      case "4":
        string=" Four ";
        break;
      case "5":
        string=" Wu ";
        break;
      case "6":
        string=" Land ";
        break;
      case "7":
        string=" Qi ";
        break;
      case "8":
        string=" Eight ";
        break;
      case "9":
        string=" Nine ";
        break;
    }
    return string;
  }
  /**
   *  Detect whether the incoming amount character is empty 
   * @param money  Amount character 
   */
  private void isNull(String money) {
    if(money == null) {
      // Throw an exception 
    }
  }
  /**
   *  Check whether the incoming amount character meets the requirements 
   * @param money  Amount character 
   */
  private void isIllegal(String money) {
    if(!money.matches("\\d+.?\\d*")) {
      // Throw an exception 
    }
  }
  /**
   *  Detect whether the incoming amount character exceeds the maximum value 
   * @param money  Amount character 
   * @return  Returns the split amount number 
   */
  private String[] isBeyondMax(String money) {
    String[] array=money.split("\\.");
    // Integer bits cannot exceed 9 Bits, and the decimal places cannot exceed 2 For 
    if(array[0].length()>9 || (array.length>1 &&array[1].length()>2)) {
      // Throw an exception 
    }
    return array;
  }
}

Related articles: