1.程序说明:将一个浮点数转换成人命币的读法字符串
2.程序流程介绍以及注意事项
流程:
*接收用户输入的一个double数值
*将这个double数值进行处理,得到整数部分和小数部分
*分别对整数部分和小数部分进行处理
*合并整数和小数字符串结果,输出结果,结束。
注意事项:
利用math.round()四舍五入
利用string.tochar()转换字符串数组
主要解决问题
字符串中零的处理,将它分为两种情况
(1)首位是否为0
不处理,一般输入的数值的首位不为0
(2)末尾的0
直接不翻译
(3)中间的单个0和连续出现的0的处理
我们每次处理4位数字,分首部的最前4位和另外的中间以及末尾4位
a.首部4位:中间单个0译为零;连续的0译为一个零,末尾的0不需要翻译
?100,连续的0到末尾的话应该也是不翻译的。
b.其他的4位:目前应该跟a.情况一样
c.全部为0的处理
翻译为零元
(5)整数和小数部分都为0的情况
(4)对于超大位数的数值进行怎样处理
目前默认处理12位
(5)根据单元的不同将数值分成每4位进行处理
1-4 单位 元
5-8 万
9-12 亿
*代码
import java.util.Scanner;
import com.sun.corba.se.spi.extension.ZeroPortPolicy;
import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Zephyr;
public class Num2Rmb
{
private double digitalValue;
private boolean zero;
String[] strHan = {"零", "壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String[] moneyCount = {"","拾","佰","仟"};
String[] moneyUnit = {"元","万","亿"};
//the default constructer
Num2Rmb()
{
digitalValue = 0.0;
zero = false;
}
//the definite constructed with the @param
Num2Rmb(double digitalRmbValue,boolean flag)
{
digitalValue = digitalRmbValue;
zero = flag;
}
//divide the digital value into the integer part and decimal part
//return the array of String[]
//string[0] is the integer part
//string[1] is the decimal part
private String[] divide(double digitalRmbValue)
{
//get the integer part of the digital money
long intPart = (long)(digitalRmbValue);
//get the decimal part of the digital money
long decimalPart = Math.round((digitalRmbValue - intPart)*100);
//another method use Integer.toString(int a)
//if the decimal part is equal to 0, then we will give 0 to the string
String strIntPart = "";
String strDecimalPart = "";
if(intPart == 0)
strIntPart = "0";
else {
strIntPart = ""+ intPart;
}
if(decimalPart<10 && decimalPart >= 0)
strDecimalPart = "0" +decimalPart;
else {
strDecimalPart = "" + decimalPart;
}
if (intPart==0 && decimalPart == 0) {
zero = true;
}
return new String[] {strIntPart,strDecimalPart };
}
//process the basic unit string with at most 12 characters
private String proDecimalRmb(String str)
{
String strResult;
char[] strRmb = new char[20];
strRmb = str.toCharArray();
if(strRmb[0]== '0' && strRmb[1] == '0')
{
strResult = "";
}
else
{
int m = strRmb[0] - '0';
int n = strRmb[1] - '0';
strResult = strHan[m]+"角"+ strHan[n]+"分";
}
return strResult;
}
//process the 4 first bit number as the form:{"456"}
//the first kind situation
private String proFirstFourBit(String str)
{
char[] firstFourBitRmb = new char[20];
firstFourBitRmb = str.toCharArray();
int len = firstFourBitRmb.length;
String strResult = "";
for(int i = 0; i < len; i++)
{
//the first zero
//if the last bit is 0 , we process it
if(firstFourBitRmb[i] == '0'&& i < len)
{
boolean zeroFlag = false;
//这个while循环表达式可能会发生错误,指针越界访问,注意表达式语句的顺序
while(i
{
zeroFlag = true;
i++;
}
if(i != len && zeroFlag == true )
strResult += "零";
i--;
}
//if non-zero bit
else
{
int m = firstFourBitRmb[i] - '0';
strResult += strHan[m] + moneyCount[len-i-1];
//the following
}
} //end for
return strResult;
}//proFirstFourBit()
//process the middle or the last four bit situation
/*private String proSecondFourBit(String str)
{
//
int i =0;
String strResult= "";
char[] secondFourBitRmb = new char[20];
secondFourBitRmb = str.toCharArray();
int len = secondFourBitRmb.length;
for(i=0; i< secondFourBitRmb.length; i++)
{
if(secondFourBitRmb[i] == '0')
{
boolean zeroFlag = false;
while(secondFourBitRmb[i] =='0'&& i< len)
{
if(zeroFlag == false)
{
zeroFlag = true;
strResult += "零";
}
i++;
}
i--;
}
//if non-zero bit
else
{
int m = secondFourBitRmb[i] - '0';
strResult += strHan[m] + moneyCount[len-i-1];
//the following
}
} //end for
return strResult;
}//proSecondFourBit()
*/ public static void main(String[] args)
{
String strIndicator = "begin to test the digital RMB";
System.out.println(strIndicator);
System.out.println("please input the digital money:");
String strTestRmb = "2356.782";
String[] strTempRmb = new String[2];
double strValue = 0D;
Scanner sc = new Scanner(System.in);
strValue = sc.nextDouble();
Num2Rmb nr = new Num2Rmb();
//divide the digital Rmb
strTempRmb = nr.divide(strValue);
//process the decimal ditial money
String result1 =nr.proDecimalRmb(strTempRmb[1]);
//process the interger part money
String result2="";
int len = strTempRmb[0].length();
if(len >12)
System.out.println("the data you input is too large to translate");
else
{
if(len>=9)
{
String strBit1 = strTempRmb[0].substring(0,len-8);
String strBit2 = strTempRmb[0].substring(len-8,len-4);
String strBit3 = strTempRmb[0].substring(len-4,len);
String strResult1 = nr.proFirstFourBit(strBit1)+"亿";
String strResult2 = nr.proFirstFourBit(strBit2)+"万";
String strResult3 = nr.proFirstFourBit(strBit3)+"元";
result2 = strResult1 + strResult2 +strResult3;
}
else
if(len>=5)
{
String strBit1 = strTempRmb[0].substring(0,len-4);
String strBit2 = strTempRmb[0].substring(len-4,len);
String strResult1 = nr.proFirstFourBit(strBit1)+"万";
String strResult2 = nr.proFirstFourBit(strBit2)+"元";
result2 = strResult1 + strResult2;
}
else
if(len>=1)
{
if (nr.zero == true)
{
result2 ="零元";
}
else
result2 = nr.proFirstFourBit(strTempRmb[0])+"元";
}
System.out.println("translate the form of the money is:");
System.out.println(result2+result1);
}
}//main()
}
//这个while循环表达式可能会发生错误,指针越界访问,注意表达式语句的顺序
while(firstFourBitRmb[i] =='0'&& i