1. 程式人生 > >金額大小寫轉換

金額大小寫轉換

import java.util.Scanner;

public class Money {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
// 金額大小寫轉換
System.out.print(“請輸入金額:”);//500
double money=input.nextDouble();
int m1, m2, m3, m4, m5, m6, temp;
temp = (int) (money * 100);
// System.out.println(money);

    m1 = temp % 1000000 / 100000;//0
    m2 = temp % 100000 / 10000;//5     百位數字
    m3 = temp % 10000 / 1000;//0
    m4 = temp % 1000 / 100;//0
    m5 = temp % 100 / 10;//0
    m6 = temp % 10;//0
  

    char c1, c2, c3, c4, c5, c6;
    c1 = transe(m1);
    c2 = transe(m2);
    c3 = transe(m3);
    c4 = transe(m4);
    c5 = transe(m5);
    c6 = transe(m6);
    String str = "";
    if (m1 != 0) {
        str += c1 + "千";
    }
    if (m2 != 0) {
        str += c2 + "百";
    }
    if (m1 != 0 && m2 == 0 && (m3 != 0 || m4 != 0)) {
        str += "零";
    }
    if (m3 != 0) {
        str += c3 + "十";
    }
    if (m2 != 0 && m3 == 0 && m4 != 0) {
        str += "零";
    }
    if (m4 != 0) {
        str += c4;
    }
    str += "元";
    if (m5 == 0 && m6 == 0) {
        str += "整";
    }
    if (m5 != 0) {
        str += c5 + "角";
    }
    if (m5 == 0 && m6 != 0) {
        str += "零";
    }
    if (m6 != 0) {
        str += c6 + "分";
    }

    System.out.print(str + "\n");
    System.out.println("..........................");

}

public static char transe(int m) {
    char r = ' ';
    switch (m) {
    case 0:
        r = '零';
        break;
    case 1:
        r = '壹';
        break;
    case 2:
        r = '貳';
        break;
    case 3:
        r = '叄';
        break;
    case 4:
        r = '肆';
        break;
    case 5:
        r = '伍';
        break;
    case 6:
        r = '陸';
        break;
    case 7:
        r = '柒';
        break;
    case 8:
        r = '捌';
        break;
    case 9:
        r = '玖';
        break;

    }
    return r;
}

}