web123456

java: Implement currency exchange (with complete source code)

import java.text.DecimalFormat; import java.util.Scanner; import javax.swing.JOptionPane; /** * Amount conversion * */ public class ConvertMoney { // Caps private final static String[] STR_NUMBER = { "zero", "one", "two", "Three", "Si", "Wu", "land", "Qi", "eight", "Nine" }; private final static String[] STR_UNIT = { "", "pickup", "Bai", "thousand", "Ten thousand", "pickup", "Bai", "thousand", "100 million", "pickup", "Bai", "thousand" };// Units of integers private final static String[] STR_UNIT2 = { "horn", "point", "Qi" };// Decimal units public static void main(String[] args) { Scanner scan = new Scanner(System.in);// Create a scanner System.out.println("Please enter an amount"); // Get the converted string String convert = convert(scan.nextDouble()); System.out.println(convert);// Output conversion result } /** * Get countable parts * * @param num * Amount * @return Amount integer part */ public static String getInteger(String num) { if (num.indexOf(".") != -1) { // Determine whether the decimal point is included num = num.substring(0, num.indexOf(".")); } num = new StringBuffer(num).reverse().toString(); // Invert the string StringBuffer temp = new StringBuffer(); // Create a StringBuffer object for (int i = 0; i < num.length(); i++) {// Join the unit temp.append(STR_UNIT[i]); temp.append(STR_NUMBER[num.charAt(i) - 48]); } num = temp.reverse().toString();// Invert the string num = numReplace(num, "Zero Pickup", "zero"); // Replace the character of the string num = numReplace(num, "Zerobai", "zero"); // Replace the character of the string num = numReplace(num, "Zero Thousand", "zero"); // Replace the character of the string num = numReplace(num, "Zero Ten Thousand", "Ten thousand"); // Replace the character of the string num = numReplace(num, "Zero Billion", "100 million"); // Replace the character of the string num = numReplace(num, "Zero Zero", "zero"); // Replace the character of the string num = numReplace(num, "hundreds of millions", "100 million"); // Replace the character of the string // Remove the string if it ends with zero if (num.lastIndexOf("zero") == num.length() - 1) { num = num.substring(0, num.length() - 1); } return num; } /** * Get the fractional part * * @param num * Amount * @return Decimal part of the amount */ public static String getDecimal(String num) { // Determine whether the decimal point is included if (num.indexOf(".") == -1) { return ""; } num = num.substring(num.indexOf(".") + 1); // Invert the string num = new StringBuffer(num).reverse().toString(); // Create a StringBuffer object StringBuffer temp = new StringBuffer(); // Join the unit for (int i = 0; i < num.length(); i++) { temp.append(STR_UNIT2[i]); temp.append(STR_NUMBER[num.charAt(i) - 48]); } num = temp.reverse().toString(); // Replace the character of the string num = numReplace(num, "Zero Angle", "zero"); // Replace the character of the string num = numReplace(num, "Zero points", "zero"); // Replace the character of the string num = numReplace(num, "Zero", "zero"); // Replace the character of the string num = numReplace(num, "Zero Zero", "zero"); // Replace the character of the string // Remove the string if it ends with zero if (num.lastIndexOf("zero") == num.length() - 1) { num = num.substring(0, num.length() - 1); } return num; } /** * Replace the contents of the string * * @param num * String * @param oldStr * Replaced content * @param newStr * New content * @return Replaced string */ public static String numReplace(String num, String oldStr, String newStr) { while (true) { // Determine whether the string contains the specified character if (num.indexOf(oldStr) == -1) { break; } // Replace string num = num.replaceAll(oldStr, newStr); } // Return the replaced string return num; } /** * Amount conversion * * @param d * Amount * @return Convert to full capital */ public static String convert(double d) { // Instantiate the DecimalFormat object DecimalFormat df = new DecimalFormat("#0.###"); // Format double numbers String strNum = df.format(d); // Determine whether the decimal point is included if (strNum.indexOf(".") != -1) { String num = strNum.substring(0, strNum.indexOf(".")); // Integer part greater than 12 cannot be converted if (num.length() > 12) { System.out.println("The number is too large to complete the conversion!"); return ""; } } String point = "";// decimal point if (strNum.indexOf(".") != -1) { point = "Yuan"; } else { point = "Yuanzhen"; } // Conversion result String result = getInteger(strNum) + point + getDecimal(strNum); if (result.startsWith("Yuan")) { // Determine whether the string has ended with "meta" result = result.substring(1, result.length()); // Intercept the string } return result; // Return a new string } }