谁能详细地给我讲解一下这个程序public class MoneyDemo {private final String[] str1 = {"元","拾","佰","仟","万","拾","佰","仟","亿"};private final String[] str2 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};pri
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/25 04:08:30
谁能详细地给我讲解一下这个程序public class MoneyDemo {private final String[] str1 = {"元","拾","佰","仟","万","拾","佰","仟","亿"};private final String[] str2 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};pri
谁能详细地给我讲解一下这个程序
public class MoneyDemo {
private final String[] str1 = {"元","拾","佰","仟","万","拾","佰","仟","亿"};
private final String[] str2 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private final String[] str3 = {"角","分"};
public static void main(String[] args){
double n = 1102001.52;
String str = new MoneyDemo().convert(n);
System.out.println(str);
}
public String convert(double n)
{
String str = String.valueOf(n);
String temp1 = str.substring(0,str.indexOf("."));
String temp2 = str.substring(str.indexOf(".")+1);
int k = -1;
int len = -1;
StringBuffer sb = new StringBuffer();
len = temp1.length()-1;
//1234
boolean flag=true;
for(int i=0; i
谁能详细地给我讲解一下这个程序public class MoneyDemo {private final String[] str1 = {"元","拾","佰","仟","万","拾","佰","仟","亿"};private final String[] str2 = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};pri
public class MoneyDemo {
\x05private final String[] str1 = { "元","拾","佰","仟","万","拾","佰","仟","亿" };
\x05private final String[] str2 = { "零","壹","贰","叁","肆","伍","陆","柒",
\x05\x05\x05"捌","玖" };
\x05private final String[] str3 = { "角","分" };
\x05public static void main(String[] args) {
\x05\x05double n = 1102001.52;
\x05\x05String str = new MoneyDemo().convert(n);
\x05\x05System.out.println(str);
\x05}
\x05public String convert(double n) {
\x05\x05String str = String.valueOf(n);//将 double 变量 n 转换成字符串
\x05\x05String temp1 = str.substring(0,str.indexOf("."));//进行切割,temp1=1102001
\x05\x05String temp2 = str.substring(str.indexOf(".") + 1);//进行切割,temp2=52
\x05\x05int k = -1;
\x05\x05int len = -1;
\x05\x05StringBuffer sb = new StringBuffer();
\x05\x05len = temp1.length() - 1;//len=6
\x05\x05// 1234
\x05\x05boolean flag = true;
\x05\x05for (int i = 0; i < temp1.length(); i++) {
\x05\x05\x05k = Integer.parseInt(temp1.substring(i,i + 1));//得到temp1的每位数字,1、1、0、2、0、0、1
\x05\x05\x05if (k != 0) {
\x05\x05\x05\x05sb.append(str2[k]).append(str1[len - i]);//str2[k]为temp1非零数的对应的大写数字、str1[len - i]为temp1拿到对应的单位
\x05\x05\x05\x05flag = true;
\x05\x05\x05} else {
\x05\x05\x05\x05if (flag) {
\x05\x05\x05\x05\x05if (len - i == 4) {
\x05\x05\x05\x05\x05\x05sb.append(str1[4]);
\x05\x05\x05\x05\x05\x05continue;
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05sb.append(str2[0]);
\x05\x05\x05\x05}
\x05\x05\x05\x05flag = false;
\x05\x05\x05}
\x05\x05}
\x05\x05for (int i = 0; i < 2; i++) {
\x05\x05\x05k = Integer.parseInt(temp2.substring(i,i + 1));//拿到少于一元的数字,5、2
\x05\x05\x05sb.append(str2[k]).append(str3[i]);//得到伍角贰分
\x05\x05}
\x05\x05return sb.toString();
\x05}
}