Skip to content

Instantly share code, notes, and snippets.

@bragil
Created March 15, 2016 14:33
Show Gist options
  • Save bragil/2c7ffeefa9fa6183ccfc to your computer and use it in GitHub Desktop.
Save bragil/2c7ffeefa9fa6183ccfc to your computer and use it in GitHub Desktop.
Cálculo do DV do CPF em C#
public string GeraDvCpf(string cpf)
{
var semDv = cpf.Substring(0, 9);
int mod11 = (semDv.ToCharArray()
.Select((c, i) => Convert.ToInt32(c.ToString()) * (10 - i))
.Sum() * 10) % 11;
// Primeiro dígito
int dig1 = mod11 > 9 ? 0 : mod11;
semDv += dig1.ToString();
mod11 = (semDv.ToCharArray()
.Select((c, i) => Convert.ToInt32(c.ToString()) * (11 - i))
.Sum() * 10) % 11;
// segundo dígito
int dig2 = mod11 > 9 ? 0 : mod11;
return string.Format("{0}{1}", dig1, dig2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment