java程式---將一個浮點數轉換成人民幣讀法,java讀法

來源:互聯網
上載者:User

java程式---將一個浮點數轉換成人民幣讀法,java讀法

代碼如下:

<span style="font-size:18px;">package test;import java.util.Scanner;//將輸入的浮點型錢數,轉成人民幣讀法public class Num2Rmb {private static double num;private long zheng,xiao;private String[] hanArr={"零","壹","貳","三","肆","伍","陸","柒","捌","玖"};private String[] danwei={"千","百","十",""};private String[] jinwei={"","萬","億"};private int[][] temp= new int[4][18];    //將輸入的錢數,轉成小數和整數部分private void swp(){zheng=(long)num;xiao=Math.round((num-zheng)*100);}//將整數部分按四位劃分成3個部分,不存在的補0private void divide(){int i,j,k=0;for(i=0;i<3;i++){long t1=zheng%10000;int t=(int) t1;zheng=zheng/10000;for(j=3;j>=0;j--){temp[i][j]=t%10;t=t/10;}}}//檢測是否要對當前的四位進行處理private int check(int i){     for(int j=0;j<4;j++)     if(temp[i][j]!=0)     return 1;     return 0;}//列印結果private void print(){//先處理整數部分    //flag用於表示當前部分是否要處理前置0int i,j,k,flag=0,cnt=0,flag2=0;for(i=2;i>=0;i--){if(check(i)==0){if(flag==1&&cnt==0){System.out.print("零");cnt++;}flag=0;continue;}//處理前置0for(j=0;j<4;j++)if(temp[i][j]!=0)break;flag2=1;if(j!=0&&flag==1)System.out.print("零");int d;//將末尾的0去掉for(d=3;d>=0;d--)if(temp[i][d]!=0)break;for(k=j;k<=d;k++){if(temp[i][k]!=0)   System.out.print(hanArr[temp[i][k]]+danwei[k]);else if(k-1>=0&&temp[i][k-1]!=0){System.out.print(hanArr[0]);}}flag=1;System.out.print(jinwei[i]);}if(flag2==1)  System.out.print("元");//處理小數部分if(xiao/10!=0)System.out.print(hanArr[(int)xiao/10]+"角");if(xiao%10!=0)System.out.print(hanArr[(int)xiao%10]+"分");System.out.println();}//對外的運行介面public void run(){swp();divide();print();}public static void main(String[] args){    Scanner sc= new Scanner(System.in);    System.out.println("請輸入錢數:");while(sc.hasNext()){num = sc.nextDouble();Num2Rmb nr =new Num2Rmb();/*//調試部分    nr.swp(); System.out.println(nr.zheng+" "+nr.xiao);    nr.divide();for(int i=2;i>=0;i--){for(int j=0;j<4;j++)System.out.print(nr.temp[i][j]);System.out.println();}nr.print();*/nr.run();    System.out.println("請輸入錢數:");}}}</span>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.