Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertoMonteiro/9f97f6b09fa8a74b0e5e to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/9f97f6b09fa8a74b0e5e to your computer and use it in GitHub Desktop.
Modulo11 para a validação de PIS
public static string Mod11(string pis)
{
var sum = 0;
var fat = 2;
for (var i = pis.Length-1; i >= 0; i--)
{
sum += Convert.ToInt32(pis[i].ToString()) * fat;
fat++;
if (fat > 9)
fat = 2;
}
var mdl = sum % 11;
if (mdl > 1)
return (11 - mdl).ToString()[0].ToString();
return "0";
}
public static string Mod11(string pis)
{
var fats = new[] { 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4 };
var mod = pis.Reverse().Zip(fats, (x, a) => (char.GetNumericValue(x) * a)).Sum() % 11;
return mod > 1 ? (11 - mod).ToString().Substring(0, 1) : "0";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment