瘋狂java講義 第四章 01人民幣的轉換讀法小程式

來源:互聯網
上載者:User

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 constructerNum2Rmb(){digitalValue = 0.0;zero = false;}//the definite constructed with the @paramNum2Rmb(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 partprivate String[] divide(double digitalRmbValue){//get the integer part of the digital moneylong intPart = (long)(digitalRmbValue);//get the decimal part of the digital moneylong 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 stringString 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 charactersprivate 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 situationprivate 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 itif(firstFourBitRmb[i] == '0'&& i < len){    boolean zeroFlag = false;    //這個while迴圈運算式可能會發生錯誤,指標越界訪問,注意運算式語句的順序while(i<len && firstFourBitRmb[i] =='0' ){             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<len)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.