Created
March 15, 2016 14:33
-
-
Save bragil/2c7ffeefa9fa6183ccfc to your computer and use it in GitHub Desktop.
Cálculo do DV do CPF em C#
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 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