《電子技術(shù)應(yīng)用》
您所在的位置:首頁 > 其他 > 業(yè)界動態(tài) > 用java實(shí)現(xiàn)大寫金額轉(zhuǎn)換及身份證15位轉(zhuǎn)18位代碼

用java實(shí)現(xiàn)大寫金額轉(zhuǎn)換及身份證15位轉(zhuǎn)18位代碼

2009-01-06
作者:孫國斌

??? 在用java作項目程序開發(fā)過程中,遇到財務(wù)制度要求在作社?;鹬Ц稌r,套打《專用付款款憑證》,金額項除阿拉伯?dāng)?shù)字外,還要求將人民幣(大寫)打印出來;另有許多參保人員,居民身份證的位數(shù)由15位更換成18位,若此類人員進(jìn)行新參保登記時,不進(jìn)行校驗(yàn)檢查,將會造成同一參保人員,以不同的居民身份證號在業(yè)務(wù)系統(tǒng)中進(jìn)行重復(fù)登記。故需提供將15位身份證轉(zhuǎn)換成18位,并檢驗(yàn)此值與核心數(shù)據(jù)庫是否有相同值,有則提示此人員已參保。另提供對18位身份證的校驗(yàn)功能(注:按已公布的國標(biāo)算法)。
模塊描述

??? (1)人民幣在大寫功能轉(zhuǎn)換,見doubleToChinese模塊,實(shí)現(xiàn)的方法為將數(shù)值及位數(shù)分別取出后,按對應(yīng)的值將其轉(zhuǎn)換成字串即可。如100,則大寫金額為“壹百整“;對負(fù)金額則加負(fù)字,例-100.01,大寫金額為“負(fù)壹百零壹分整”。

??? (2)身份證由15位轉(zhuǎn)18位,可見fixPersonIDCode模塊。附:提交判斷是否為有效的18位身份證號isIdentityId模塊,方法為判斷年份為19開頭的18位身份證號,將其轉(zhuǎn)化為15位后,按國標(biāo)進(jìn)行修補(bǔ),若經(jīng)轉(zhuǎn)換的值同輸入的18位值相同,則說明身份證編號是有效的,否則身份證號可能有誤;考慮到2000年以后出生的人的身份證編號,可能到2016年以后才能申請到,到時此系統(tǒng)能否還在使用很難說,故暫未提供對其身份證號碼的判斷功能。但在此也提供解決的辦法,可以先判斷18位身份證中的年份,對以20開頭的年份,進(jìn)行轉(zhuǎn)換時修補(bǔ)程序中改為加20既可。
??? 目前以上功能模塊在社保核心業(yè)務(wù)系統(tǒng)應(yīng)用正常。
??? 本例用jbuilder7編譯通過。說明:以下代碼中所加注的說明,說明功能模塊的作用。在具體應(yīng)用中借鑒或拷貝此代碼到實(shí)際程序中即可使用。
package csi.utilitytools;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.*;
import java.io.ByteArrayInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

public class COMUT {

public COMUT() {
? }

? /**
?? * 修補(bǔ)15位居民身份證號碼為18位
?? * @param personIDCode
?? * @return
?? */
? public static String fixPersonIDCode(String personIDCode)
? {
??? String retIDCode = "";
??? if ( personIDCode == null || personIDCode.trim().length() != 15 ) {
????? return personIDCode;
??? }
??? String id17 = personIDCode.substring(0,6) + "19" + personIDCode.substring(6,15);? //15為身份證補(bǔ)'19'

??? char[] code = {'1','0','X','9','8','7','6','5','4','3','2'};? //11個
??? int[] factor={0, 2,4,8, 5,10,9,7, 3,6,1,2, 4,8,5,10, 9,7}; //18個;
??? int[] idcd = new int[18];
??? int???? i;
??? int???? j;
??? int???? sum;
??? int???? remainder;

??? for (i=1; i<18; i++)
??? {
????? j = 17 - i ;
????? idcd[i] = Integer.parseInt(id17.substring(j, j+1));
??? }

??? sum = 0;
??? for (i=1; i<18; i++)
??? {
????? sum = sum? + idcd[i] * factor[i];
??? }
??? remainder = sum%11;
??? String lastCheckBit = String.valueOf(code[ remainder ]);
??? return id17 + lastCheckBit;
??? }

??? /**
???? *人民幣大寫金額轉(zhuǎn)換
???? * @param number
???? * @param pattern
???? * @return
???? * @throws GeneralException
???? */
??? public static String format(double number,String pattern)
??? {
????? DecimalFormat df = new DecimalFormat(pattern);
????? return df.format(number);
??? }
??? public static String format(String number,String pattern)
??? {
????? double d = new Double(number).doubleValue();
????? return format(d,pattern);
??? }
??? public static String doubleToChinese(double sumofcash)
??? {
????? String[] arr = {"分","角","元","拾","百","千","萬","拾萬","百萬",
??????????????????????????????? "千萬","億","拾億","百億","千億"};
????? String sTmp = "";
????? String sMoney = "";
????? String header = "";
????? if(sumofcash<0)
??????? header = "負(fù)";
????? sumofcash = Math.abs(sumofcash);
????? double dTmp =sumofcash*100;
????? sMoney = format(dTmp,"0,000.#");
????? StringTokenizer st = new StringTokenizer(sMoney,",");
????? String tmp = "";
????? while (st.hasMoreElements()){
??????? tmp = tmp + st.nextToken();
????? }
????? sMoney = tmp;
????? int iLen = sMoney.length();
????? int count = 0;
????? for (int i=0;i????? {
??????? String sTemp = sMoney.substring(i,i+1);
??????? if (sTemp.equals("0"))
??????? {
????????? count++;
????????? if (count==1)
????????? {
??????????? sTmp = sTmp + trans(sTemp);
????????? }
??????? }else
??????? {
????????? sTmp = sTmp + trans(sTemp);
????????? sTmp = sTmp + arr[iLen-i-1];
????????? count = 0;
??????? }

????? }
????? sTmp = sTmp.trim();
????? iLen = sTmp.length();
????? if (sTmp.substring(iLen-1,iLen).equals("零"))
????? {
??????? sTmp = sTmp.substring(0,iLen-1);
????? }
????? sTmp = sTmp + "整";
????? sTmp = header.concat(sTmp);
????? return sTmp;
??? }
??? private static String trans(String args)
??? {
????? int iTemp = Integer.parseInt(args);
????? String sRes = "";
????? switch (iTemp)
????? {
??????? case 1:sRes = "壹";
????????? break;
??????? case 2:sRes = "貳";
????????? break;
??????? case 3:sRes = "叁";
????????? break;
??????? case 4:sRes = "肆";
????????? break;
??????? case 5:sRes = "伍";
????????? break;
??????? case 6:sRes = "陸";
????????? break;
??????? case 7:sRes = "柒";
????????? break;
??????? case 8:sRes = "捌";
????????? break;
??????? case 9:sRes = "玖";
????????? break;
??????? case 0:sRes = "零";
????? }
????? return sRes;
??? }


/**
?* 判斷是否是有效的18位或15位居民身份證號碼
?* @param identityId:18位或15位居民身份證號碼
?* @return:true: 有效的18位或15位居民身份證號碼
?*/
??? public static boolean isIdentityId(String identityId) {
????? if(isEmpty(identityId)) return false;
????? try{
??????? if(identityId.length()==18){
????????? String identityId15 = identityId.substring(0,6)+identityId.substring(8,17);
????????? //System.out.println("the identityId15 is : "+identityId15);
????????? if(fixPersonIDCode(identityId15).equalsIgnoreCase(identityId)) {
??????????? return true;
????????? }else {
????????? return false;
????????? }
??????? }else if(identityId.length()==15){
????????? try{
??????????? Long.parseLong(identityId);
??????????? return true;
????????? }catch(Exception ex){
??????????? return false;
????????? }
??????? }else{
????????? return false;
??????? }
????? }catch(Exception ex){
??????????? return false;
????? }

? }

? /**
?? * 判斷是否為空串""
?? */
? public static boolean isEmpty(String sValue) {
??? if(sValue==null) return true;
??? return sValue.trim().equals("") ? true : false ;
?}

? public static void main(String[] args) {

??? try{
????? System.out.println(doubleToChinese(12003000.01)); //打印金額大寫
System.out.println(fixPersonIDCode("650103760113073"));//身份證15位轉(zhuǎn)18位
System.out.println(isIdentityId ("650103760113073")); //否是為有效的15位身份證號

??? } catch (Exception e) {
????? e.printStackTrace();
??? }
? }
}

程序調(diào)試結(jié)果如下:
D:\JBuilder7\jdk1.3.1\bin\javaw -classpath "D:\ejbwork\xjsb\classes;D:\bea\wlserver6.0\lib\weblogic.jar;C:\bea\wlserver6.1\lib\weblogic_sp.jar;C:\bea\wlserver6.1\lib\weblogic.jar;D:\JBuilder7\jdk1.3.1\demo\jfc\Java2D\Java2Demo.jar;D:\JBuilder7\jdk1.3.1\jre\lib\i18n.jar;D:\JBuilder7\jdk1.3.1\jre\lib\jaws.jar;D:\JBuilder7\jdk1.3.1\jre\lib\rt.jar;D:\JBuilder7\jdk1.3.1\jre\lib\sunrsasign.jar;D:\JBuilder7\jdk1.3.1\lib\dt.jar;D:\JBuilder7\jdk1.3.1\lib\htmlconverter.jar;D:\JBuilder7\jdk1.3.1\lib\tools.jar"? -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=zw2k:1992,suspend=y
?csi.utilitytools. COMUT
壹千萬貳百萬零叁千零壹分整650103197601130732
true

本站內(nèi)容除特別聲明的原創(chuàng)文章之外,轉(zhuǎn)載內(nèi)容只為傳遞更多信息,并不代表本網(wǎng)站贊同其觀點(diǎn)。轉(zhuǎn)載的所有的文章、圖片、音/視頻文件等資料的版權(quán)歸版權(quán)所有權(quán)人所有。本站采用的非本站原創(chuàng)文章及圖片等內(nèi)容無法一一聯(lián)系確認(rèn)版權(quán)者。如涉及作品內(nèi)容、版權(quán)和其它問題,請及時通過電子郵件或電話通知我們,以便迅速采取適當(dāng)措施,避免給雙方造成不必要的經(jīng)濟(jì)損失。聯(lián)系電話:010-82306118;郵箱:aet@chinaaet.com。