Created
January 14, 2016 07:46
-
-
Save eltabo/e9fa5fc1e5dd8c2140b4 to your computer and use it in GitHub Desktop.
Cálculo del dígito de control del código de municipio INE
This file contains hidden or 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
public class DigitoControl { | |
private static final int[][] magic = { | |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, | |
{0, 3, 8, 2, 7, 4, 1, 5, 9, 6}, | |
{0, 2, 4, 6, 8, 1, 3, 5, 7, 9} | |
}; | |
public static int calc(int test) { | |
byte[] bytes = String.format("%05d", test).getBytes(); | |
int sum = 0, i = 0; | |
for(Byte v : bytes){ | |
sum+=magic[2 - i % 3][v - 48]; | |
i++; | |
} | |
return sum == 0?0:10 - sum % 10; | |
} | |
public static void main(String[] args) { | |
System.out.println(calc(1001)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment