Detailed explanation of java digital to Chinese character tool class

  • 2021-07-22 09:41:01
  • OfStack

In this paper, we share the specific codes of java digital to Chinese character tool class for your reference. The specific contents are as follows


/**
 * Created by 33303 on 2017/7/28.
 */
import java.math.BigDecimal;

/**
 *  Convert numbers into capitals of RMB in Chinese <br>
 *
 */
public class NumberToCN {
 /**
 *  Capitalized digits in Chinese 
 */
 private static final String[] CN_UPPER_NUMBER = { " Zero ", " One ", " 2 ", " 3 ", " Four ",
  " Wu ", " Land ", " Qi ", " Eight ", " Nine " };
 /**
 *  In Chinese, monetary units are capitalized, which is similar to placeholders 
 */
 private static final String[] CN_UPPER_MONETRAY_UNIT = { " Points ", " Angle ", " Yuan ",
  " Pick up ", " Bai ", " Thousands ", " Ten thousand ", " Pick up ", " Bai ", " Thousands ", " Billion ", " Pick up ", " Bai ", " Thousands ", " Mega ", " Pick up ",
  " Bai ", " Thousands " };
 /**
 *  Special character: whole 
 */
 private static final String CN_FULL = " Whole ";
 /**
 *  Special character: negative 
 */
 private static final String CN_NEGATIVE = " Negative ";
 /**
 *  Accuracy of the amount, the default value is 2
 */
 private static final int MONEY_PRECISION = 2;
 /**
 *  Special character: Zero whole 
 */
 private static final String CN_ZEOR_FULL = " Zero element " + CN_FULL;

 /**
 *  Convert the input amount into the capital of RMB in Chinese 
 *
 * @param numberOfMoney
 *   Amount entered 
 * @return  Corresponding Chinese capitalization 
 */
 public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
 StringBuffer sb = new StringBuffer();
 // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
 // positive.
 int signum = numberOfMoney.signum();
 //  The case of zero yuan whole 
 if (signum == 0) {
  return CN_ZEOR_FULL;
 }
 // The amount will be used here 4 Shed 5 Into 
 long number = numberOfMoney.movePointRight(MONEY_PRECISION)
  .setScale(0, 4).abs().longValue();
 //  Get two decimal places 
 long scale = number % 100;
 int numUnit = 0;
 int numIndex = 0;
 boolean getZero = false;
 //  Judge the last two digits, 1 Shared 4 Medium situation: 00 = 0, 01 = 1, 10, 11
 if (!(scale > 0)) {
  numIndex = 2;
  number = number / 100;
  getZero = true;
 }
 if ((scale > 0) && (!(scale % 10 > 0))) {
  numIndex = 1;
  number = number / 10;
  getZero = true;
 }
 int zeroSize = 0;
 while (true) {
  if (number <= 0) {
  break;
  }
  //  Every time you get to the end, 1 Number 
  numUnit = (int) (number % 10);
  if (numUnit > 0) {
  if ((numIndex == 9) && (zeroSize >= 3)) {
   sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
  }
  if ((numIndex == 13) && (zeroSize >= 3)) {
   sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
  }
  sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  sb.insert(0, CN_UPPER_NUMBER[numUnit]);
  getZero = false;
  zeroSize = 0;
  } else {
  ++zeroSize;
  if (!(getZero)) {
   sb.insert(0, CN_UPPER_NUMBER[numUnit]);
  }
  if (numIndex == 2) {
   if (number > 0) {
   sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
   }
  } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
   sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
  }
  getZero = true;
  }
  //  Jean number Remove the last every time 1 Number 
  number = number / 10;
  ++numIndex;
 }
 //  If signum == -1 The number entered is negative, and a special character is appended to the front: negative 
 if (signum == -1) {
  sb.insert(0, CN_NEGATIVE);
 }
 //  The number entered is two decimal places after the decimal point "00" In the case of, a special character should be appended at the end: whole 
 if (!(scale > 0)) {
  sb.append(CN_FULL);
 }
 return sb.toString();
 }

 public static void main(String[] args) {
 double money = 2020004.01;
 BigDecimal numberOfMoney = new BigDecimal(money);
 String s = NumberToCN.number2CNMontrayUnit(numberOfMoney);
 System.out.println(" The amount you entered is: " "+ money +" "  #--# [" +s.toString()+"]");
 }
}

Related articles: