Last active
January 17, 2025 04:47
-
-
Save melizeche/1b649909fffe14e7c3b034c035b4b3f5 to your computer and use it in GitHub Desktop.
Calculo del dígito verificador del RUC en Javascript
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
/** | |
* Calcula el dígito verificador del RUC utilizando el algoritmo basado en el módulo 11 (Cálculo de la DNIT/SET). | |
* Ref: https://www.dnit.gov.py/documents/20123/224893/D%C3%ADgito+Verificador.pdf/fb9f86c8-245d-9dad-2dc1-ac3b3dc307a7?t=1683343426554.pdf | |
* | |
* @author Marcelo Elizeche Landó | |
* @license MIT | |
* | |
* @param {number} numero - Número base(CI o RUC sin DV) para calcular el dígito verificador. | |
* @param {number} [basemax=11] - Base máxima para los multiplicadores (por defecto 11, ver PDF de la DNIT). | |
* @returns {number} Dígito verificador calculado. | |
*/ | |
function calcularDigitoVerificador(numero, basemax = 11) { | |
const numeroStr = String(numero); | |
let total = 0; | |
let k = 2; | |
for (let i = numeroStr.length - 1; i >= 0; i--) { | |
total += Number(numeroStr[i]) * k; | |
k = (k === basemax) ? 2 : k + 1; | |
} | |
const resto = total % 11; | |
return resto > 1 ? 11 - resto : 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment