import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 读入一个浮点数值,将其转化为中文金额
的大写形式。如
123.45,转化为:壹佰贰拾叁元肆角伍分
* 作者:wzw
* 日期:2017年6月1日
*/
//HuoBiZhuanHuan货币转换
public class HuoBiZhuanHuan {
public static void main(String[] args) {
HuoBiZhuanHuan hbzh = new HuoBiZhuanHuan();
hbzh.myMethod();
}
public void myMethod(){
int zhengshu;
float xiaoshu;
Scanner input = new Scanner(System.in);
System.out.println("请输入金钱数>");
double money = input.nextDouble();
if(money > 0 && money < 100000){
zhengshu = (int) money;
xiaoshu = (float) (money-zhengshu);
if(xiaoshu > 0){
zhuanHuanZhengShu(money);
System.out.print("元");
zhuanHuanXiaoShu(xiaoshu);
}else{
zhuanHuanZhengShu(money);
System.out.print("元整");
}
}else{
System.out.println("超出转换的范围限制!");
}
input.close();
}
//转换整数部分
public void zhuanHuanZhengShu(double munber) {
// TODO Auto-generated method stub
Integer myint = new Integer((int) munber);
List<String> zhengshuList = new ArrayList<String>();
int mun;
if((myint/10000) != 0){
mun = myint/10000;
String str = turnMunber(mun);
str += "万";
zhengshuList.add(str);
}if(myint/1000 != 0){
mun = (myint/1000)%10;
String str = turnMunber(mun);
str += "仟";
zhengshuList.add(str);
}if(myint/100 != 0){
mun = (myint/100)%10;
String str = turnMunber(mun);
str += "佰";
zhengshuList.add(str);
}if(myint/10 != 0){
mun = (myint/10)%10;
String str = turnMunber(mun);
str += "拾";
zhengshuList.add(str);
}if(munber>=0){
mun = (myint%10);
String str = turnMunber(mun);
zhengshuList.add(str);
}
for(int i=0;i<zhengshuList.size();i++){
if(zhengshuList.get(i).startsWith("零")){
if(i == (zhengshuList.size()-1)){
zhengshuList.remove(i);
zhengshuList.add(i,"");
}if(zhengshuList.get(i).startsWith("零") && zhengshuList.get((i+1)).startsWith("零")){
zhengshuList.remove(i);
zhengshuList.add(i,"");
}if(zhengshuList.get(i).startsWith("零") && !(zhengshuList.get((i+1)).startsWith("零"))){
System.out.print("零");
}
}else{
System.out.print(zhengshuList.get(i));
}
}
}
//转换小数部分
public void zhuanHuanXiaoShu(float xiaoshu){
List<String> xiaoshuList = new ArrayList<String>();
int mun;
if((xiaoshu*10) > 0){
mun = (int) (xiaoshu*10);
String str = turnMunber(mun);
str += "角";
xiaoshuList.add(str);
}
if((xiaoshu*100) > 0){
mun = (int) (xiaoshu*100)%10;
String str = turnMunber(mun);
str += "分";
xiaoshuList.add(str);
}
for(int j=0;j<xiaoshuList.size();j++){
if(xiaoshuList.get(j).startsWith("零")){
xiaoshuList.remove(j);
xiaoshuList.add(j,"");
}else{
System.out.print(xiaoshuList.get(j));
}
}
}
//判断数值转换中文
public static String turnMunber(int mun){
switch (mun) {
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 "玖";
}
return null;
}
}
import java.util.List;
import java.util.Scanner;
/**
* 读入一个浮点数值,将其转化为中文金额
的大写形式。如
123.45,转化为:壹佰贰拾叁元肆角伍分
* 作者:wzw
* 日期:2017年6月1日
*/
//HuoBiZhuanHuan货币转换
public class HuoBiZhuanHuan {
public static void main(String[] args) {
HuoBiZhuanHuan hbzh = new HuoBiZhuanHuan();
hbzh.myMethod();
}
public void myMethod(){
int zhengshu;
float xiaoshu;
Scanner input = new Scanner(System.in);
System.out.println("请输入金钱数>");
double money = input.nextDouble();
if(money > 0 && money < 100000){
zhengshu = (int) money;
xiaoshu = (float) (money-zhengshu);
if(xiaoshu > 0){
zhuanHuanZhengShu(money);
System.out.print("元");
zhuanHuanXiaoShu(xiaoshu);
}else{
zhuanHuanZhengShu(money);
System.out.print("元整");
}
}else{
System.out.println("超出转换的范围限制!");
}
input.close();
}
//转换整数部分
public void zhuanHuanZhengShu(double munber) {
// TODO Auto-generated method stub
Integer myint = new Integer((int) munber);
List<String> zhengshuList = new ArrayList<String>();
int mun;
if((myint/10000) != 0){
mun = myint/10000;
String str = turnMunber(mun);
str += "万";
zhengshuList.add(str);
}if(myint/1000 != 0){
mun = (myint/1000)%10;
String str = turnMunber(mun);
str += "仟";
zhengshuList.add(str);
}if(myint/100 != 0){
mun = (myint/100)%10;
String str = turnMunber(mun);
str += "佰";
zhengshuList.add(str);
}if(myint/10 != 0){
mun = (myint/10)%10;
String str = turnMunber(mun);
str += "拾";
zhengshuList.add(str);
}if(munber>=0){
mun = (myint%10);
String str = turnMunber(mun);
zhengshuList.add(str);
}
for(int i=0;i<zhengshuList.size();i++){
if(zhengshuList.get(i).startsWith("零")){
if(i == (zhengshuList.size()-1)){
zhengshuList.remove(i);
zhengshuList.add(i,"");
}if(zhengshuList.get(i).startsWith("零") && zhengshuList.get((i+1)).startsWith("零")){
zhengshuList.remove(i);
zhengshuList.add(i,"");
}if(zhengshuList.get(i).startsWith("零") && !(zhengshuList.get((i+1)).startsWith("零"))){
System.out.print("零");
}
}else{
System.out.print(zhengshuList.get(i));
}
}
}
//转换小数部分
public void zhuanHuanXiaoShu(float xiaoshu){
List<String> xiaoshuList = new ArrayList<String>();
int mun;
if((xiaoshu*10) > 0){
mun = (int) (xiaoshu*10);
String str = turnMunber(mun);
str += "角";
xiaoshuList.add(str);
}
if((xiaoshu*100) > 0){
mun = (int) (xiaoshu*100)%10;
String str = turnMunber(mun);
str += "分";
xiaoshuList.add(str);
}
for(int j=0;j<xiaoshuList.size();j++){
if(xiaoshuList.get(j).startsWith("零")){
xiaoshuList.remove(j);
xiaoshuList.add(j,"");
}else{
System.out.print(xiaoshuList.get(j));
}
}
}
//判断数值转换中文
public static String turnMunber(int mun){
switch (mun) {
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 "玖";
}
return null;
}
}