Last active
October 7, 2021 09:13
-
-
Save rasputino/87cc8878d3729fec87bd1440dd4efbbf to your computer and use it in GitHub Desktop.
Cálculo del código de control del INE en C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var codigo = 66108;//España | |
//Cálculo de código de control del INE | |
var A = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | |
var B = new int[] { 0, 3, 8, 2, 7, 4, 1, 5, 9, 6 }; | |
var C = new int[] { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9}; | |
codigo = Convert.ToInt32(codigo.ToString().Reverse().Aggregate(0, (b, x) => 10 * b + x - '0')); | |
var columna = 0; | |
var sum = 0; | |
while(codigo > 0){ | |
var digito = codigo%10; | |
codigo = codigo/10; | |
switch (columna) | |
{ | |
case 0: sum += C[digito]; break; | |
case 1: sum += B[digito]; break; | |
case 2: sum += A[digito]; break; | |
} | |
columna++; | |
if(columna == 3) columna = 0; | |
} | |
var restantePara10 = sum % 10; | |
if(restantePara10 == 0) restantePara10 = 10; | |
var resultado = 10 - restantePara10; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment