實現數字向人民幣大寫轉換

最近,在一個銀行項目中接觸到把數字向人民幣大寫轉換的問題。其實也並不難,我們需要一個方法把數字分割成個位,十位,百位等進行替換。

下面是一個實現兩位數的數字向人民幣大寫方式的轉換案例:

 public Object execute(Object[] args) throws Exception {
    	  int amout = ((Integer)args[0]).intValue();
    	    String[] str = { "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖", "拾" };

    	    String result = "";
    	    if (amout <= 10) {
    	      result = str[(amout - 1)] + "元";
    	    } else {
    	      int ten = amout / 10;
    	      int gewei = amout % 10;

    	      String shiWei = "";
    	      if (ten == 1)
    	        shiWei = "拾";
    	      else {
    	        shiWei = str[(ten - 1)] + "拾";
    	      }
    	      if (gewei == 0)
    	        result = shiWei + "元";
    	      else {
    	        result = shiWei + str[(gewei - 1)] + "元";
    	      }
    	    }

    	    System.out.println(result);
    	    return result;
    }
在main方法中,進行調用
 public static void main(String[] args) throws Exception {
    	Question1 a = new Question1();
    	  a.execute(new Object[] { Integer.valueOf(4) });
    	    a.execute(new Object[] { Integer.valueOf(10) });
    	    a.execute(new Object[] { Integer.valueOf(11) });
    	    a.execute(new Object[] { Integer.valueOf(25) });
    	    a.execute(new Object[] { Integer.valueOf(89) });
    	    a.execute(new Object[] { Integer.valueOf(90) });
    }

發佈了35 篇原創文章 · 獲贊 74 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章