Skip to content

Instantly share code, notes, and snippets.

@dilnei
Created August 6, 2014 17:18
Show Gist options
  • Select an option

  • Save dilnei/c5bb833dd3d1b1591788 to your computer and use it in GitHub Desktop.

Select an option

Save dilnei/c5bb833dd3d1b1591788 to your computer and use it in GitHub Desktop.
Utilitários genéricos para geração de módulo11
package br.com.risingforce.geradores;
/**
* <b>Contém metodos utilitários genéricos para geração de módulo11.</b>
*
* @author Dilnei Cunha.
*/
public final class UtilsModulo11 {
/**
* <b>Calcula o módulo 11 de um número qualquer.</b>
*
* @param numberstr
* @return int
*/
public static int mod11(String numberstr) {
final Class<?>[] expectedClasses = {Integer.class, Long.class};
final String str = numberstr;
int sum = 0;
int fact = 2;
int digit;
for (int i = str.length() - 1; i >= 0; i--) {
sum += Integer.parseInt(str.substring(i, i + 1)) * fact;
fact++;
fact = fact > 9 ? 2 : fact;
}
digit = sum % 11;
if (digit == 1) {
digit = 0;
} else if (digit == 0) {
digit = 1;
} else {
digit = 11 - digit;
}
return digit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment