国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現將數字轉換成人民幣大寫

2019-11-26 15:13:49
字體:
來源:轉載
供稿:網友

Rmb.java

public class Rmb {  /**   *人民幣的基本信息和操作   *@author weinee   *@version 1.0  */  double number;     //人民幣的數量  private String[] hanArr = {"零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" };//漢字計數符號  private String[] unitArr = {"", "拾", "佰", "仟"};//漢字計數單位  private String[] unitArrs = {"萬", "億", "萬", "兆", "萬", "億", "萬", "圓"}; //順序添加單位  //private String[] unitsArr = {"萬", "億"}; //漢字計數大單位  public Rmb(){}  /**   *構造器初始化人民幣數量   *@param 給定的初始化人民幣數  */  public Rmb(double number){    this.number = number;  }   /**   *把一個浮點數分解成long型部分和小數部分字符串,把人民幣換成了整數,浮點數在取小數部分時臨界值有誤差   *@return 返回分解后的字符串數組,第一個數組元素是整數部分,第二個是小數部分字符串  */  public String[] divideNum(){    double num = Math.round(number*100);//對number進行四舍五入取整    long integerPart = (long)num; //連同小數點后兩位取成整數 ,避免使用減法    //double decimalsPart = num-integerPart;//小數部分,臨界時產生了誤差    String decimalsPartStr;    long b = integerPart % 10; //小數點后第二位    long a = (integerPart/10) % 10;//小數點后第一位    integerPart /= 100;    if(a==0 && b==0){      decimalsPartStr = null;    }else{      decimalsPartStr = "" + a + b;    }    return new String[] {String.valueOf(integerPart) , decimalsPartStr};  }   /**   *把一個數字字符轉換成漢語的人民幣讀法   *@return 返回漢語人民幣讀法的字符串形式  */  public String toHanStr(){    String[] results = new String[9]; //用來暫時存儲每四位分段后的數字字符串    String[] resultStrs = new String[9];//用來暫時存儲每四位分段后轉換成的人民幣讀法    String result = "";//最終的轉換結果    String[] divideStr = divideNum(); //得到浮點數分解成的long型部分和小數部分字符串,第一個數組元素是整數部分字符串,第二個是小數部分字符串    results[8] = divideStr[1];    for (int i=divideStr[0].length(), j=8; i>0&&j>0 ; i-=4,j--){      try{        results[j-1] = divideStr[0].substring(i-4, i);      }catch(Exception e){        results[j-1] = divideStr[0].substring(0, i);        break;      }    }     if(results[8] == null){      resultStrs[8] = "整";    }else if(results[8].charAt(1) == '0'){      resultStrs[8] = hanArr[results[8].charAt(0) - 48] + "角"; //根據ASCII碼和hanArr數組吧數字改成漢語大寫    }else{      resultStrs[8] = hanArr[results[8].charAt(0) - 48] + "角" + hanArr[results[8].charAt(1) - 48] + "分";     }    for(int i=0; i<8; i++){      if(results[i] != null){        resultStrs[i] = "";        resultStrs[i] += hanArr[results[i].charAt(0) - 48] + unitArr[results[i].length() - 1]; //根據ASCII碼和數組長度選擇數的單位        for (int j=1; j<results[i].length(); j++ )          if(results[i].charAt(j-1) == '0' && results[i].charAt(j) != '0')            resultStrs[i] += "零" + hanArr[results[i].charAt(j) - 48] + unitArr[results[i].length() - 1 - j];  //根據ASCII碼和數組長度選擇數的單位          else if(results[i].charAt(j) != '0' )            resultStrs[i] += hanArr[results[i].charAt(j) - 48] + unitArr[results[i].length() - 1 - j];      }    }    for (int i=0; i<8; i++ ){      if(resultStrs[i] != null){        result += resultStrs[i] + unitArrs[i];      }    }    result += resultStrs[8];    return result;  }}

RmbTest.java

public class RmbTest{  public static void main(String[] args)   {    double l;    byte[] bye = new byte[50];     System.out.println("請輸入要轉換的人民幣數量:");    try{      System.in.read(bye);    }catch(Exception e){}    String s = new String(bye);    l = Double.parseDouble(s);    Rmb r = new Rmb(l);    s = r.toHanStr();    System.out.println(s);     }}

以上所述就是本文的全部內容了,希望對大家學習java能夠有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙里县| 宿迁市| 汕尾市| 麻江县| 宁河县| 安吉县| 宣武区| 德江县| 鄂尔多斯市| 时尚| 新疆| 怀集县| 勃利县| 双流县| 琼海市| 宕昌县| 孟村| 勐海县| 沙坪坝区| 固阳县| 襄汾县| 平顺县| 武穴市| 阳泉市| 西畴县| 拜泉县| 昆山市| 监利县| 邵阳县| 清苑县| 平塘县| 镇安县| 东乡族自治县| 德惠市| 江西省| 安远县| 玉龙| 无棣县| 临泽县| 浙江省| 明光市|