SoFunction
Updated on 2025-04-15

Java implements the complete process of converting digital amounts to Chinese capital amounts

1. Functional Overview

This program converts the integer amount (0~99999999) entered by the user into a Chinese capital amount that complies with financial specifications, and automatically adds unit symbols (such as "10,000", "10,000", "10," etc.). For example, input1234, outputOne thousand two hundred three shinings

2. Core code analysis

1. Enter the verification module

int inputMoney;
while (true) {
    ("Please enter the amount:");
    int temp = ();
    if (temp >= 0 && temp <= 9999999) {
        inputMoney = temp;
        break;
    } else {
        ("The amount entered is incorrect, please re-enter!");
    }
}
  • Function: Ensure that the input amount is within the legal range (0~9,999,999)
  • Highlights: Use infinite loop to force the user to enter a legal value
  • Risk points: Non-numeric input is not processed (if input letters will crash)

2. Digital splitting and capital conversion

String result = "";
while(true) {
    int temp = inputMoney % 10;   // Take the last number    inputMoney = inputMoney / 10; 
    String resultString = toBigChange(temp); // Convert to capital    result = resultString + result; // Reverse order splicing    if (inputMoney == 0) break;
}
  • Algorithm logic
    • By taking the rest operation%10Get the last digit
    • By deletion/10Remove the last position that has been processed
    • Reverse splicing ensures correct number order

3. Zero processing (seven-digit alignment)

int len = 7 - ();
for(int i = 0; i < len; i++) {
    result = "zero" + result;
}
  • Example:Enter123→ Complement zero asZero zero zero one two three
  • effect: Provide a unified length basis for subsequent addition of unit symbols

4. Add amount unit

public static String addSymbolHandle(String string) {
    String[] symbolArray = {"Bai", "pickup", "Ten thousand", "thousand", "Bai", "pickup", "Yuan"};
    String resultString = "";
    for(int i = 0; i < (); i++) {
        char s = (i);
        resultString += s + symbolArray[i];
    }
    return resultString;
}

Unit comparison table

index 0   1    2    3    4    5    6
    Bai  pickup   Ten thousand   thousand   Bai   pickup   Yuan
  • Output exampleZero-10,000,23,000

3. Key methods description

1. How to convert numbers to capitalization

public static String toBigChange(int number) {
    String[] bigArray = {"zero", "one", "two", "Three", "Si", "Wu", "land", "Qi", "eight", "Nine"};
    return bigArray[number];
}
  • Mapping rules: Directly convert through array subscript
  • Things to note: Relying on input verification ensuresnumberBetween 0 and 9

4. Run example demonstration

enter1234Processing process

  • Split the numbers:1 2 3 4
  • Convert capitalization:1 2 3 si
  • Complement zero:Zero zero one two three
  • Add units:Zero-10,000,23,000

Final output

Zero Zero One Two Three
Zero-Bailing Shilling, One Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thousand Thous

5. Code optimization suggestions

1. Input verification enhancement

try {
    int temp = ();
} catch (InputMismatchException e) {
    ("Please enter the number!");
    (); // Clear non-digital input    continue;
}

2. Unit order adjustment

Order of units that are more in line with Chinese customs:

String[] symbolArray = {"Bai", "pickup", "Ten thousand", "thousand", "Bai", "pickup", "Yuan"};

3. Complement the logic optimization

// Use format to make up for zeroresult = ("%07d", (result));

6. Expanding thinking

  • How to support a larger amount(such as billions)?

    • Adding unit array:{"Yi", "Qian", "Bai", "Se", "Wan", ...}
  • How to deal with decimal amounts(Yuanjiao Fen)?

    • Split integer and decimal parts to process separately
    • Supplementary units:"Angle", "Section"
  • How to remove excess zero values

    • Regular replacement:("Zero[Qianbaise]", "Zero")

7. Source code

package ;

import ;

public class StringTest6 {
    public static void main(String[] MakeItPossible) {
        // Integer disassembly        Scanner sc = new Scanner();
        int inputMoney;
        while (true) {
            ("Please enter the amount:");
            int temp = ();
            if (temp >= 0 && temp <= 9999999) {
                inputMoney = temp;
                break;
            } else {
                ("The amount entered is incorrect, please re-enter!");
            }
        }

        // After splitting the amount, the caller will convert it to capitalized amount, and then splicing it        String result = "";
        while(true) {
            int temp = inputMoney % 10;   // Get the leftmost number            inputMoney = inputMoney / 10; // Delete the leftmost number            // Pass this number to the function to get the capitalization of this number            String resultString = toBigChange(temp);
            // Rely on the returned capital letters, pay attention to the order (disassembly is from right to left, and splicing is from left to right)            result = resultString + result;
            if (inputMoney == 0) {
                // The processed data has reached 0, which means that the disassembly is completed and terminates directly!                break;
            }
        }
        // Seven digits of the amount, less than the left side to make up zero        int len = 7 - ();
        for(int i = 0; i < len; i++) {
            result = "zero" + result;
        }
        (result + " ");
        (addSymbolHandle(result) + " ");
    }

    /**
      * toBigChange method: Return the capitalization of the number according to the passed number
      * @param number
      * @return String
      */
    public static String toBigChange(int number) {
        String[] bigArray = {"zero", "one", "two", "Three", "Si", "Wu", "land", "Qi", "eight", "Nine"};
        return bigArray[number];
    }

    /**
      * addSymbolHandle method: insert unit according to amount
      * @param string
      * @return String
      */
    public static String addSymbolHandle(String string) {
        String[] symbolArray = {"Bai", "pickup", "Ten thousand", "thousand", "Bai", "pickup", "Yuan"};
        String resultString = "";
        for(int i = 0; i < (); i++) {
            // 1. Divide the string into characters            char s = (i);
            // 2. Splice the characters into the amount units, accumulate them and assign them to the new string storage            resultString = resultString + s + symbolArray[i];
        }
        return resultString;
    }
}

8. Summary

Through this case, we have mastered:

  • Digital disassembly and reverse order splicingTips
  • Chinese amount unitAdd rules
  • Enter verificationBasic implementation method

The above is the detailed content of the complete process of Java from numbers to Chinese capital amounts. For more information about Java numbers to Chinese capital amounts, please pay attention to my other related articles!