62,614
社区成员
发帖
与我相关
我的任务
分享
import java.text.DecimalFormat;
import java.util.Scanner;
public class ChineseCurrency {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("please input the price:");
double number = scan.nextDouble();
System.out.println(toChineseCurrency(new Double(number)));
}
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();
}
}}