62,623
社区成员
发帖
与我相关
我的任务
分享import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
public class ChineseCurrency{
public static void main(String[] args) {
try{
System.out.print("请输入数值:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String st=(String)br.readLine();
double number=Double.parseDouble(st);
System.out.println(toChineseCurrency(new Double(number)));
}catch(IOException e){}
}
public static String toChineseCurrency(Object o) {
if(o instanceof Number) {
String s =new DecimalFormat("#.00").format(o);
System.out.println(s);
s =s.replaceAll("\\.", "");
char[] digit ={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";
int l=unit.length();
StringBuffer sb=new StringBuffer(unit);
for(int i=s.length()-1;i>=0; i--)
sb=sb.insert(l-s.length()+i,digit[(s.charAt(i)-0x30)]);
s=sb.substring(l-s.length(),l+s.length());
s=s.replaceAll("零[拾佰仟]","零"). replaceAll("零{2,}","零").
replaceAll("零([兆万元])","$1").replaceAll("零[角分]","");
return s;
} else{
throw new NumberFormatException();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NumberOutput {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bfr = new BufferedReader(isr);
String input = bfr.readLine();
int number = 0;
try {
number = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("数字格式错误");
System.exit(1);
}
int[] intArr = new int[20];
String[] strArr = new String[20];
int i = 0;
while (number > 0) {
intArr[i] = number % 10;
i++;
number /= 10;
}
StringBuffer buf = new StringBuffer();
for (int j = intArr.length - 1; j >= 0; j--) {
if (intArr[j] != 0) {
buf.append(getChinese(intArr[j]));
buf.append(getPos(j));
}
}
System.out.println(buf);
}
public static String getChinese(int i) {
switch (i) {
case 0:
return "零";
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
default:
return "";
}
}
public static String getPos(int i) {
switch (i) {
case 0:
return "";
case 1:
return "十";
case 2:
return "百";
case 3:
return "千";
case 4:
return "万";
case 5:
return "十万";
case 6:
return "百万";
case 7:
return "千万";
case 8:
return "亿";
case 9:
return "十亿";
default:
return "";
}
}
}