计算LUHN算法模10“隔位2倍加”校验数的公式
计算步骤如下:
步骤1:从右边第1个数字(低序)开始每隔一位乘以2。
步骤2:把在步骤1中获得的乘积的各位数字与原号码中未乘2的各位数字相加。
步骤3:从邻近的较高的一个以0结尾的数中减去步骤2中所得到的总和[这相当于求这个总和的低位数字(个位数)的“10的补数”]。如果在步骤2得到的总和是以零结尾的数(如30、40等等),则校验数字就是零。
例:
无校验数字的卡号 4992 73 9871
4 9 9 2 7 3 9 8 7 1
×2 ×2 ×2 ×2 ×2
18 4 6 16 2
4+1+8+9+4+7+6+9+1+6+7+2=64
70-64=6
带有校验数字的卡号为:4992 73 9871 6
excel当中的校验公式:假设卡号在A2单元格中,公式可以计算出账号最后一位的校验位数字
=MOD(SUM(-MID(TEXT(MMULT((0&MID(LEFT(A2,LEN(A2)-1),ROW($1:$9)*2-MOD(LEN(A2)+{0,1},2),1))*{1,2},{100;1}),"000"),{1,2,3},1)),10)
数组公式,要按shift+ctrl+enter结束
求魔方当中的公式应该怎样写呢?
(银联要求的卡号是13-19位此代码要满足13-19位卡号的计算)
答复:
完整支持16-19位银号卡号校验位计算:
text(mod((left(right($MF(账号)$,3),1)+left(right($MF(账号)$,5),1)+left(right($MF(账号)$,7),1)+left(right($MF(账号)$,9),1)+left(right($MF(账号)$,11),1)+left(right($MF(账号)$,13),1)+left(right($MF(账号)$,15),1)+mod(left(right($MF(账号)$,2),1)*2,10)+int(left(right($MF(账号)$,2),1)*2/10)+mod(left(right($MF(账号)$,4),1)*2,10)+int(left(right($MF(账号)$,4),1)*2/10)+mod(left(right($MF(账号)$,6),1)*2,10)+int(left(right($MF(账号)$,6),1)*2/10)+mod(left(right($MF(账号)$,8),1)*2,10)+int(left(right($MF(账号)$,8),1)*2/10)+mod(left(right($MF(账号)$,10),1)*2,10)+int(left(right($MF(账号)$,10),1)*2/10)+mod(left(right($MF(账号)$,12),1)*2,10)+int(left(right($MF(账号)$,12),1)*2/10)+mod(left(right($MF(账号)$,14),1)*2,10)+int(left(right($MF(账号)$,14),1)*2/10)+mod(left(right($MF(账号)$,16),1)*2,10)+int(left(right($MF(账号)$,16),1)*2/10)+if(len($MF(账号)$)>=17,left(right($MF(账号)$,17),1),0)+if(len($MF(账号)$)>=18,mod(value(left(right($MF(账号)$,18),1))*2,10)+int(value(left(right($MF(账号)$,18),1))*2/10),0)+if(len($MF(账号)$)>=19,left(right($MF(账号)$,19),1),0))*9,10),0)
配合银行BIN码的检查,就可以知道银行卡是出自哪个银行。
功德圆满
16-19位的校验位公式
text(mod((left(right($MF(账号)$,3),1)+left(right($MF(账号)$,5),1)+left(right($MF(账号)$,7),1)+left(right($MF(账号)$,9),1)+left(right($MF(账号)$,11),1)+left(right($MF(账号)$,13),1)+left(right($MF(账号)$,15),1)+mod(left(right($MF(账号)$,2),1)*2,10)+int(left(right($MF(账号)$,2),1)*2/10)+mod(left(right($MF(账号)$,4),1)*2,10)+int(left(right($MF(账号)$,4),1)*2/10)+mod(left(right($MF(账号)$,6),1)*2,10)+int(left(right($MF(账号)$,6),1)*2/10)+mod(left(right($MF(账号)$,8),1)*2,10)+int(left(right($MF(账号)$,8),1)*2/10)+mod(left(right($MF(账号)$,10),1)*2,10)+int(left(right($MF(账号)$,10),1)*2/10)+mod(left(right($MF(账号)$,12),1)*2,10)+int(left(right($MF(账号)$,12),1)*2/10)+mod(left(right($MF(账号)$,14),1)*2,10)+int(left(right($MF(账号)$,14),1)*2/10)+mod(left(right($MF(账号)$,16),1)*2,10)+int(left(right($MF(账号)$,16),1)*2/10)+if(len($MF(账号)$)>=17,left(right($MF(账号)$,17),1),0)+if(len($MF(账号)$)>=18,mod(value(left(right($MF(账号)$,18),1))*2,10)+int(value(left(right($MF(账号)$,18),1))*2/10),0)+if(len($MF(账号)$)>=19,left(right($MF(账号)$,19),1),0))*9,10),0)
package com.cup.tracy;
public class Luhn {
/**
* Luhn算法
* 根据卡号获取校验位
* @param cardNumber
* @return
*/
public static int getCheckNumber(String cardNumber){
int totalNumber = 0;
int lastNumber = 0;
for (int i = cardNumber.length()-1; i >= 0; i-=2) {
int tmpNumber = calculate(Integer.parseInt(String.valueOf(cardNumber.charAt(i))) * 2);
if (i==0) {
totalNumber += tmpNumber;
}else {
totalNumber += tmpNumber + Integer.parseInt(String.valueOf(cardNumber.charAt(i-1)));
}
}
if (totalNumber >= 0 && totalNumber < 9) {
return (10 - totalNumber);
}else {
String str = String.valueOf(totalNumber);
if (Integer.parseInt(String.valueOf(str.charAt(str.length()-1))) == 0) {
return 0;
}else {
return (10 - Integer.parseInt(String.valueOf(str.charAt(str.length()-1))));
}
}
}
/**
* 计算数字各位和
* @param number
* @return
*/
public static int calculate(int number){
String str = String.valueOf(number);
int total = 0;
for (int i = 0; i < str.length(); i++) {
total += Integer.valueOf(Integer.parseInt(String.valueOf(str.charAt(i))));
}
return total;
}
public static void main(String[] args) {
String cardNumber = "4992739871";
System.out.println(getCheckNumber(cardNumber));
}
}