谁能详细地给我讲解一下这个程序public class MoneyDemo {private final String[

1个回答

  • public class MoneyDemo {

    x05private final String[] str1 = { "元","拾","佰","仟","万","拾","佰","仟","亿" };

    x05private final String[] str2 = { "零","壹","贰","叁","肆","伍","陆","柒",

    x05x05x05"捌","玖" };

    x05private final String[] str3 = { "角","分" };

    x05public static void main(String[] args) {

    x05x05double n = 1102001.52;

    x05x05String str = new MoneyDemo().convert(n);

    x05x05System.out.println(str);

    x05}

    x05public String convert(double n) {

    x05x05String str = String.valueOf(n);//将 double 变量 n 转换成字符串

    x05x05String temp1 = str.substring(0,str.indexOf("."));//进行切割,temp1=1102001

    x05x05String temp2 = str.substring(str.indexOf(".") + 1);//进行切割,temp2=52

    x05x05int k = -1;

    x05x05int len = -1;

    x05x05StringBuffer sb = new StringBuffer();

    x05x05len = temp1.length() - 1;//len=6

    x05x05// 1234

    x05x05boolean flag = true;

    x05x05for (int i = 0; i < temp1.length(); i++) {

    x05x05x05k = Integer.parseInt(temp1.substring(i,i + 1));//得到temp1的每位数字,1、1、0、2、0、0、1

    x05x05x05if (k != 0) {

    x05x05x05x05sb.append(str2[k]).append(str1[len - i]);//str2[k]为temp1非零数的对应的大写数字、str1[len - i]为temp1拿到对应的单位

    x05x05x05x05flag = true;

    x05x05x05} else {

    x05x05x05x05if (flag) {

    x05x05x05x05x05if (len - i == 4) {

    x05x05x05x05x05x05sb.append(str1[4]);

    x05x05x05x05x05x05continue;

    x05x05x05x05x05}

    x05x05x05x05x05sb.append(str2[0]);

    x05x05x05x05}

    x05x05x05x05flag = false;

    x05x05x05}

    x05x05}

    x05x05for (int i = 0; i < 2; i++) {

    x05x05x05k = Integer.parseInt(temp2.substring(i,i + 1));//拿到少于一元的数字,5、2

    x05x05x05sb.append(str2[k]).append(str3[i]);//得到伍角贰分

    x05x05}

    x05x05return sb.toString();

    x05}

    }