In Java, converting the price of a number to Chinese capital usually involves several steps, including decomposing the number into various parts (such as single digits, ten digits, hundreds digits, etc.), then converting it into corresponding Chinese capital numbers based on these parts, and adding appropriate units (such as element, angle, and fraction).
Here is an example method that takes a BigDecimal type price (units in 1, containing two decimal places) and converts it to Chinese capital:
import ; public class PriceConverter { private static final String[] CHINESE_NUMBERS = {"zero", "one", "two", "Three", "Si", "Wu", "land", "Qi", "eight", "Nine"}; private static final String[] UNITS = {"", "pickup", "Bai", "thousand"}; private static final String[] BIG_UNITS = {"", "Ten thousand", "100 million", "mega", "Beijing", "boundary", "trillion", "Ryo", "ditch", "Stream", "just", "load", "pole"}; // More large units can be added as needed public static String convertToChinese(BigDecimal price) { if (price == null || () < 0) { throw new IllegalArgumentException("The price cannot be empty or less than 0"); } // Convert to a string and cut off the part before and after the decimal point String priceStr = (2, BigDecimal.ROUND_HALF_UP).toPlainString(); String integerPart = ("\\.")[0]; // Integer part String decimalPart = ("\\.")[1]; // Decimal part (angle, part) // Process integer parts String integerChinese = convertToChinesePart(integerPart, UNITS, BIG_UNITS); // Process the fractional part String decimalChinese = ""; if (!"00".equals(decimalPart)) { decimalChinese = convertToDecimalChinese(decimalPart); } // Merge results return integerChinese + "Yuan" + decimalChinese; } private static String convertToChinesePart(String numberStr, String[] units, String[] bigUnits) { if ("0".equals(numberStr)) { return CHINESE_NUMBERS[0]; } StringBuilder sb = new StringBuilder(); int unitIndex = 0; // int zeroCount = 0; for (int i = () - 1; i >= 0; i--) { int digit = (i) - '0'; String chineseDigit = CHINESE_NUMBERS[digit]; if (digit == 0) { // zeroCount++; // Continuous zero processing: only add a zero after a non-zero number, at the unit change, or at the beginning of a string if (() > 0 && ((() - 1) != CHINESE_NUMBERS[0].charAt(0) || unitIndex == 0 || i == 0)) { (0, chineseDigit); } } else { // Add non-zero numbers and corresponding units (0, chineseDigit + units[unitIndex]); // zeroCount = 0; // Reset the count of consecutive zeros } // Switch to the next unit if (++unitIndex == ) { unitIndex = 0; // Recycle the unit array if (() > 0 && i > 0) { (0, bigUnits[(()-i) / ]); } } } // Remove possible extra zeros at the end while (() > 0 && (0) == CHINESE_NUMBERS[0].charAt(0)) { (0); } return (); } private static String convertToDecimalChinese(String decimalPart) { StringBuilder sb = new StringBuilder(); if ((0) != '0') { (CHINESE_NUMBERS[(0) - '0']).append("horn"); } if (() > 1 && (1) != '0') { (CHINESE_NUMBERS[(1) - '0']).append("point"); } return (); } public static void main(String[] args) { (convertToChinese(new BigDecimal("4545444444111141233123522212345.67"))); // 5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (convertToChinese(new BigDecimal("100.00"))); // One hundred yuan (convertToChinese(new BigDecimal("0.01"))); // Zero yuan per cent (convertToChinese(new BigDecimal("0.00"))); // Zero yuan } }
Attachment: Java converts the entered number amount into Chinese capital amount
Convert each numeric character in the input numeric string to the corresponding Chinese capital, for example, 12345 to 12335, and then splice the Chinese capital and the corresponding units and output them together.
The output result is: 12,300 yuan
package ; import ; public class Demo1 { //Get the Chinese capitalization corresponding to each number public static String getCapitalNumber(int number){ String []capitalnumber={"zero","one","two","Three","Si","Wu","land","Qi","eight","Nine"}; return capitalnumber[number]; } public static void main(String[] args) { //Put each digit of the input amount into the array int []sz=new int[7]; Scanner sc=new Scanner(); int money; while (true){ ("Please enter any amount:"); money=(); if(money>=0 ||money<=9999999) { break; }else { ("The amount is invalid, please re-enter"); } } String capmoney=""; int i=0; while (true){ int ge=money%10; sz[i++]=ge; money=money/10; if(money==0) break; } //Get the string of capital numbers String capitalnumber=""; for (int j = 0; j < ; j++) { capitalnumber=getCapitalNumber(sz[j]); capmoney=capitalnumber+capmoney; } //Define a string array storage unit String []arr={"Bai","pickup","Ten thousand","thousand","Bai","pickup","Yuan"}; //Split strings of capital numbers and unit arrays String result=""; for (int k = 0; k < (); k++) { char c=(k); result=result+c+arr[k]; } (result); } }
Summarize
This is the end of this article about converting java digital prices to Chinese capitals. For more related content on converting java digital prices to Chinese capitals, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!