C#中華民國營利事業統一編號檢核(統編 檢查)
C#中華民國營利事業統一編號檢核(統編 檢查)
資料來源: https://slashview.com/archive2022/20221113.html
https://dotblogs.com.tw/ChentingW/2020/03/29/000036
http://superlevin.ifengyuan.tw/%E7%87%9F%E5%88%A9%E4%BA%8B%E6%A5%AD%E7%B5%B1%E4%B8%80%E7%B7%A8%E8%99%9F%E9%82%8F%E8%BC%AF%E6%AA%A2%E6%9F%A5%E6%96%B9%E6%B3%95/
code01
public bool CheckUID(string idNo) { if (idNo == null) { return false; } Regex regex = new Regex(@"^\d{8}$"); Match match = regex.Match(idNo); if (!match.Success) { return false; } int[] idNoArray = idNo.ToCharArray().Select(c => Convert.ToInt32(c.ToString())).ToArray(); int[] weight = new int[] { 1, 2, 1, 2, 1, 2, 4, 1 }; int subSum; //小和 int sum = 0; //總和 int sumFor7 = 1; for (int i = 0; i < idNoArray.Length; i++) { subSum = idNoArray[i] * weight[i]; sum += (subSum / 10) //商數 + (subSum % 10); //餘數 } if (idNoArray[6] == 7) { //若第7碼=7,則會出現兩種數值都算對,因此要特別處理。 sumFor7 = sum + 1; } return (sum % 10 == 0) || (sumFor7 % 10 == 0); }
code02
//請注意這是新版的規則喔,舊版的規則請自行將% 5改回% 10,不過我想時至今日應該沒有這個需求了吧,程式碼如下: public static (bool bIsError, string cMessage) CheckTaxID(string cTax) { //回傳結果集 var oResult = (bIsError: false, cMessage: "統一編號格式合法"); //邏輯乘數(財政部制定) var cMagic = "12121241"; try { if (string.IsNullOrEmpty(cTax) || cTax.Length != 8 || !int.TryParse(cTax, out int iUnused)) { throw new System.Exception("請輸入八位數純數字"); } //轉成數值陣列 var aryTax = cTax.ToCharArray().Select(x => (int)(x - '0')).ToArray(); var aryMagic = cMagic.ToCharArray().Select(x => (int)(x - '0')).ToArray(); //運算乘積 var aryResult = new int[8]; for (int i = 0; i < aryTax.Length; i++) { aryResult[i] = aryTax[i] * aryMagic[i]; } //運算整理:大於10就進行位數相加 aryResult = aryResult.Select(x => x < 10 ? x : x.ToString().ToCharArray().Select(y => (int)(y - '0')).Sum()).ToArray(); //運算整理:第七位數大於10之分拆 var oList = new System.Collections.Generic.List<int[]>(); foreach (var cItem in aryResult[6].ToString().ToCharArray()) { var aryTemp = aryResult.ToArray(); aryTemp[6] = (int)(cItem - '0'); oList.Add(aryTemp); } //運算整理:乘積和與除5判斷 if (!oList.Select(x => x.Sum()).Select(x => x % 5 == 0).Any(x => x)) { throw new System.Exception("統一編號格式不合法"); } } catch (System.Exception oEx) { oResult.bIsError = true; oResult.cMessage = oEx.Message; } return oResult; } public static void Main() { //04595257;合法性:True / 訊息:統一編號格式合法 var cTax = "04595257"; var oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //04595252;合法性:True / 訊息:統一編號格式合法 cTax = "04595252"; oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //10458575;合法性:True / 訊息:統一編號格式合法 cTax = "10458575"; oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //10458574;合法性:True / 訊息:統一編號格式合法 cTax = "10458574"; oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //10458570;合法性:True / 訊息:統一編號格式合法 cTax = "10458570"; oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //22099131;合法性:True / 訊息:統一編號格式合法 cTax = "22099131"; //台積電 oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //08878978;合法性:True / 訊息:統一編號格式合法 cTax = "08878978"; //亂打:舊版錯誤、新版正確 oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); //10458573;合法性:False / 訊息:統一編號格式不合法 cTax = "10458573"; //亂打:舊版錯誤、新版錯誤 oResult = CheckTaxID(cTax); WriteLine($"{cTax};合法性:{!oResult.bIsError} / 訊息:{oResult.cMessage}"); }