package test;
import java.util.Scanner;
/*
* 3、编写程序将表示钱数的数字转换为大写形式,例如;1200.53 壹仟贰佰零元伍角三分;
*/
public class ChangeMoney {
public static final String[] CN = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
public static final String[] UNIT = { "分", "角", "元", "十", "佰", "仟", "万", "十", "佰", "仟", "亿", "十", "佰", "仟", "兆",
"十", "佰", "仟" };
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
double money = scan.nextDouble();
// System.out.println(money - 0.05);
System.out.println(moneyToCn(money));
}
private static String moneyToCn(double money) {
String rs = "";
if (money == 0) {
return "零元整";
}
long num = (long) (Math.round(money * 100));// 所有有效位转为整型;浮点数运算可能有误差,使用Math.round四舍五入
System.out.println(num);
int index = 0;// 记录当前处理的位
int currNum;// 当前处理的数字
boolean hasZero = false;// 是否出现过零
int zero = 0;
long decimal = num % 100;
if (decimal == 0) {
index = 2;// 没有角、分直接从元开始
num = num / 100;
hasZero = true;
} else if (decimal % 10 == 0) {
index = 1;// 没有分
num = num / 10;
hasZero = true;
}
while (true) {
if (num <= 0) {
break;// 所有的数字处理完成,退出循环
}
currNum = (int) (num % 10);// 取当前处理位的数字
if (currNum > 0) {// currNum不是0
if (index == 9 && zero >= 3) {
rs = "万" + rs;
}
if (index == 13 && zero >= 3) {
rs = "亿" + rs;
}
if (index == 17 && zero >= 3) {
rs = "兆" + rs;
}
rs = UNIT[index] + rs;// 先加单位
rs = CN[currNum] + rs;// 加大写数字
hasZero = false;
} else {// currNum是0
if (!hasZero) {// 之前有一位数不是零
rs = CN[currNum] + rs;
}
// 第一位是0需要元;万、亿、兆位之后有数字的需要单位
if (index == 2 || ((index - 2) % 4 == 0) && num % 1000 != 0) {
rs = UNIT[index] + rs;
}
hasZero = true;
zero++;
}
num = num / 10;// 处理完成舍去最后一位
index++;
}
if (decimal == 0) {// 没有角、分需要加上整
rs += "整";
}
return rs;
}
}
import java.util.Scanner;
/*
* 3、编写程序将表示钱数的数字转换为大写形式,例如;1200.53 壹仟贰佰零元伍角三分;
*/
public class ChangeMoney {
public static final String[] CN = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
public static final String[] UNIT = { "分", "角", "元", "十", "佰", "仟", "万", "十", "佰", "仟", "亿", "十", "佰", "仟", "兆",
"十", "佰", "仟" };
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
double money = scan.nextDouble();
// System.out.println(money - 0.05);
System.out.println(moneyToCn(money));
}
private static String moneyToCn(double money) {
String rs = "";
if (money == 0) {
return "零元整";
}
long num = (long) (Math.round(money * 100));// 所有有效位转为整型;浮点数运算可能有误差,使用Math.round四舍五入
System.out.println(num);
int index = 0;// 记录当前处理的位
int currNum;// 当前处理的数字
boolean hasZero = false;// 是否出现过零
int zero = 0;
long decimal = num % 100;
if (decimal == 0) {
index = 2;// 没有角、分直接从元开始
num = num / 100;
hasZero = true;
} else if (decimal % 10 == 0) {
index = 1;// 没有分
num = num / 10;
hasZero = true;
}
while (true) {
if (num <= 0) {
break;// 所有的数字处理完成,退出循环
}
currNum = (int) (num % 10);// 取当前处理位的数字
if (currNum > 0) {// currNum不是0
if (index == 9 && zero >= 3) {
rs = "万" + rs;
}
if (index == 13 && zero >= 3) {
rs = "亿" + rs;
}
if (index == 17 && zero >= 3) {
rs = "兆" + rs;
}
rs = UNIT[index] + rs;// 先加单位
rs = CN[currNum] + rs;// 加大写数字
hasZero = false;
} else {// currNum是0
if (!hasZero) {// 之前有一位数不是零
rs = CN[currNum] + rs;
}
// 第一位是0需要元;万、亿、兆位之后有数字的需要单位
if (index == 2 || ((index - 2) % 4 == 0) && num % 1000 != 0) {
rs = UNIT[index] + rs;
}
hasZero = true;
zero++;
}
num = num / 10;// 处理完成舍去最后一位
index++;
}
if (decimal == 0) {// 没有角、分需要加上整
rs += "整";
}
return rs;
}
}