求用Java编写一程序,实现货币的大小写之间的转换。 5
1个回答
展开全部
import java.util.Scanner;
public class Change {
public static void main(String agrs[]) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入金额:");
System.out.println(digitUppercase(sc.nextDouble()));
}
public static String digitUppercase(double n){
String fraction[] = {"角", "分","毫","厘"};
String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String unit[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};
String head = n < 0? "负": "";
n = Math.abs(n);
String s = "";
for(int i = 0; i < fraction.length; i++) {
s += (digit[(int)(Math.floor(n * 10* Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
}
if(s.length()<1){
s = "整";
}
int integerPart = (int)Math.floor(n);
for(int i = 0; i < unit[0].length && integerPart > 0; i++) {
String p ="";
for(int j = 0; j < unit[1].length && n > 0; j++) {
p = digit[integerPart%10]+unit[1][j] + p;
integerPart = integerPart/10;
}
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
}
return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
}
}
public class Change {
public static void main(String agrs[]) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入金额:");
System.out.println(digitUppercase(sc.nextDouble()));
}
public static String digitUppercase(double n){
String fraction[] = {"角", "分","毫","厘"};
String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String unit[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};
String head = n < 0? "负": "";
n = Math.abs(n);
String s = "";
for(int i = 0; i < fraction.length; i++) {
s += (digit[(int)(Math.floor(n * 10* Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
}
if(s.length()<1){
s = "整";
}
int integerPart = (int)Math.floor(n);
for(int i = 0; i < unit[0].length && integerPart > 0; i++) {
String p ="";
for(int j = 0; j < unit[1].length && n > 0; j++) {
p = digit[integerPart%10]+unit[1][j] + p;
integerPart = integerPart/10;
}
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
}
return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
}
}
追问
如果是大写转换成小写怎么办?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询