Last active
March 1, 2019 19:35
-
-
Save ismaelgasparin/623e37bd3ed13275ccb1b66ff5e05c59 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Linq; | |
public class Program | |
{ | |
private static string GetDigitoVerificadorIBGE(string codigo) | |
{ | |
var pesos = new[] { 1, 2, 1, 2, 1, 2 }; | |
var codigoStr = codigo.ToString(); | |
var soma = 0; | |
for (var i = 0; i < codigoStr.Length; i++) | |
{ | |
var pon = (codigoStr[i] - '0') * pesos[i]; | |
if (pon > 9) | |
{ | |
pon = pon.ToString().Select(c => c - '0').Sum(); | |
} | |
soma += pon; | |
} | |
var resto = soma % 10; | |
var digito = resto == 0 ? 0 : (10 - resto); | |
return digito.ToString(); | |
} | |
public static void Main() | |
{ | |
var codigos = new [] { "280030", "150140", "310620", "140010", "530010", "500270", "510340", "410690" }; | |
foreach (var codigo in codigos) | |
{ | |
Console.WriteLine("Código: " + codigo + " - Digito: " + GetDigitoVerificadorIBGE(codigo)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment