程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL2008 >> java完成整數轉化為中文大寫金額的辦法

java完成整數轉化為中文大寫金額的辦法

編輯:MSSQL2008

java完成整數轉化為中文大寫金額的辦法。本站提示廣大學習愛好者:(java完成整數轉化為中文大寫金額的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成整數轉化為中文大寫金額的辦法正文


在日常生活中,我們常常會將阿拉伯數字轉化為中文大寫的狀況:"零", "壹", "貳", "三", "肆", "伍", "陸", "柒", "捌", "玖", "拾", "佰", "仟", "萬", "億",因而自己就編寫一個類,完成該功用的轉化。
完成進程:
關於這個問題的完成,我們首先把數字按四位一個拆開(契合我們讀數字的習氣),然後對這四位數,做出他的讀法,詳細代碼如下:

private static String getNumberStr1000 (int num) { 
 if (num > 9999 || num < 0) { 
  return ""; 
 } 
 int q = num / 1000; 
 int b = (num / 100) % 10; 
 int s = (num / 10) % 10; 
 int g = num % 10; 
 StringBuffer sb = new StringBuffer(); 
 //千 
 if (q > 0) { 
  sb.append(NUMBER1[q]); 
  sb.append(NUMBER2[3]); 
 } 
 //百 
 if (b > 0) { 
  sb.append(NUMBER1[b]); 
  sb.append(NUMBER2[2]); 
 } else { 
  if (q != 0) { 
   sb.append(NUMBER2[0]); 
  } 
 } 
 //十 
 if (s > 0) { 
  sb.append(NUMBER1[s]); 
  sb.append(NUMBER2[1]); 
 } else { 
  if (b != 0) { 
   sb.append(NUMBER2[0]); 
  } 
 } 
 //個 
 if (g > 0) { 
  sb.append(NUMBER1[g]); 
 } 
 return sb.toString(); 
} 

上面我們只需求在編寫一個辦法,完成每個四位數的讀法以及對應的單位即可,詳細代碼如下:

public static String getNumberStr(int num) { 
 if (num < 0) { 
  return ""; 
 } 
 if (num == 0) { 
  return NUMBER1[0]; 
 } 
 int split = 10000; 
 int y = num / (split * split); 
 int w = (num / split) % split; 
 int g = num % split; 
 StringBuffer sb = new StringBuffer(); 
 //億 
 if (y > 0) { 
  sb.append(getNumberStr1000(y)); 
  sb.append(NUMBER2[5]); 
 } 
 //萬 
 if (w > 999) { 
  sb.append(getNumberStr1000(w)); 
  sb.append(NUMBER2[4]); 
 } else { 
  if (w > 0) { 
   if (y != 0) { 
    sb.append(NUMBER2[0]); 
   } 
   sb.append(getNumberStr1000(w)); 
   sb.append(NUMBER2[4]); 
  } 
 } 
 //萬以下 
 if (g > 0) { 
  if (w != 0) { 
   if (g > 999) { 
    sb.append(getNumberStr1000(g)); 
   } else { 
    sb.append(NUMBER2[0]); 
    sb.append(getNumberStr1000(g)); 
   } 
    
  } else { 
   if (y != 0) { 
    sb.append(NUMBER2[0]); 
   } 
   sb.append(getNumberStr1000(g)); 
  } 
 } 
 return sb.toString(); 
} 

完好源代碼:

 /** 
 *@Description:  
 */ 
package com.lulei.util; 
 
public class NumberUtil { 
 private static String[] NUMBER1 = {"零", "壹", "貳", "三", "肆", "伍", "陸", "柒", "捌", "玖"}; 
 private static String[] NUMBER2 = {"零", "拾", "佰", "仟", "萬", "億"}; 
  
 /** 
  * @param num 
  * @return 
  * @Author:lulei 
  * @Description:將數字轉化為大寫 
  */ 
 public static String getNumberStr(int num) { 
  if (num < 0) { 
   return ""; 
  } 
  if (num == 0) { 
   return NUMBER1[0]; 
  } 
  int split = 10000; 
  int y = num / (split * split); 
  int w = (num / split) % split; 
  int g = num % split; 
  StringBuffer sb = new StringBuffer(); 
  //億 
  if (y > 0) { 
   sb.append(getNumberStr1000(y)); 
   sb.append(NUMBER2[5]); 
  } 
  //萬 
  if (w > 999) { 
   sb.append(getNumberStr1000(w)); 
   sb.append(NUMBER2[4]); 
  } else { 
   if (w > 0) { 
    if (y != 0) { 
     sb.append(NUMBER2[0]); 
    } 
    sb.append(getNumberStr1000(w)); 
    sb.append(NUMBER2[4]); 
   } 
  } 
  //萬以下 
  if (g > 0) { 
   if (w != 0) { 
    if (g > 999) { 
     sb.append(getNumberStr1000(g)); 
    } else { 
     sb.append(NUMBER2[0]); 
     sb.append(getNumberStr1000(g)); 
    } 
     
   } else { 
    if (y != 0) { 
     sb.append(NUMBER2[0]); 
    } 
    sb.append(getNumberStr1000(g)); 
   } 
  } 
  return sb.toString(); 
 } 
  
 /** 
  * @param num 
  * @return 
  * @Description:對萬以下的數字停止大小寫轉化 
  */ 
 private static String getNumberStr1000 (int num) { 
  if (num > 9999 || num < 0) { 
   return ""; 
  } 
  int q = num / 1000; 
  int b = (num / 100) % 10; 
  int s = (num / 10) % 10; 
  int g = num % 10; 
  StringBuffer sb = new StringBuffer(); 
  //千 
  if (q > 0) { 
   sb.append(NUMBER1[q]); 
   sb.append(NUMBER2[3]); 
  } 
  //百 
  if (b > 0) { 
   sb.append(NUMBER1[b]); 
   sb.append(NUMBER2[2]); 
  } else { 
   if (q != 0) { 
    sb.append(NUMBER2[0]); 
   } 
  } 
  //十 
  if (s > 0) { 
   sb.append(NUMBER1[s]); 
   sb.append(NUMBER2[1]); 
  } else { 
   if (b != 0) { 
    sb.append(NUMBER2[0]); 
   } 
  } 
  //個 
  if (g > 0) { 
   sb.append(NUMBER1[g]); 
  } 
  return sb.toString(); 
 } 
 
 public static void main(String[] args) { 
  // TODO Auto-generated method stub 
  int i = 1; 
  System.out.println(i + "--" + NumberUtil.getNumberStr(i)); 
  i = 1001; 
  System.out.println(i + "--" + NumberUtil.getNumberStr(i)); 
  i = 100101; 
  System.out.println(i + "--" + NumberUtil.getNumberStr(i)); 
  i = 10100101; 
  System.out.println(i + "--" + NumberUtil.getNumberStr(i)); 
  i = 1234567890; 
  System.out.println(i + "--" + NumberUtil.getNumberStr(i)); 
 } 
 
}

 測試運轉後果:

希望本文所述對大家學習java順序設計有所協助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved