Last active
September 12, 2023 02:43
-
-
Save jesobreira/5f29f0e58249d18ec8234af5806926ee to your computer and use it in GitHub Desktop.
Digito verificador em PHP e 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
/** | |
* Calculates the Modulo 11 for generating the verification digit of bank slips and bank accounts. | |
* | |
* @author Pablo Costa <[email protected]> | |
* @link www.febraban.org.br | |
* | |
* @param {string} num Numeric string for which the verification digit is to be calculated. | |
* @param {number} [base=9] Maximum value of multiplication. | |
* @param {number} [r=0] Specifies the return type. If 0, returns the verification digit, if 1 returns the remainder. | |
* | |
* @returns {number} Returns the verification digit or remainder based on the r parameter. | |
* | |
* @note Script developed without any reuse of pre-existing code. | |
* @note It's assumed that the verification of the format of the input variables is done before executing this script. | |
*/ | |
function modulo11(num, base = 9, r = 0) { | |
let sum = 0; | |
let factor = 2; | |
// Separation of numbers | |
for (let i = num.length; i > 0; i--) { | |
// Get each number individually | |
let currentNum = parseInt(num.charAt(i - 1)); | |
// Multiply the number by the factor | |
let partial = currentNum * factor; | |
// Sum of the digits | |
sum += partial; | |
if (factor === base) { | |
// Reset multiplication factor to 2 | |
factor = 1; | |
} | |
factor++; | |
} | |
// Calculation of modulo 11 | |
if (r === 0) { | |
sum *= 10; | |
let digit = sum % 11; | |
if (digit === 10) { | |
digit = 0; | |
} | |
return digit; | |
} else if (r === 1) { | |
let remainder = sum % 11; | |
return remainder; | |
} | |
} |
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
<?php | |
/** | |
* Calculates the Modulo 11 for generating the verification digit of bank slips and bank accounts. | |
* | |
* @author Pablo Costa <[email protected]> | |
* @link www.febraban.org.br | |
* | |
* @param string $num Numeric string for which the verification digit is to be calculated. | |
* @param int $base Maximum value of multiplication. Defaults to 9. | |
* @param int $r Specifies the return type. If 0, returns the verification digit, if 1 returns the remainder. Defaults to 0. | |
* | |
* @return int Returns the verification digit or remainder based on the $r parameter. | |
* | |
* @note Script developed without any reuse of pre-existing code. | |
* @note It's assumed that the verification of the format of the input variables is done before executing this script. | |
*/ | |
function modulo_11($num, $base = 9, $r = 0) { | |
$sum = 0; | |
$factor = 2; | |
// Separation of numbers | |
for ($i = strlen($num); $i > 0; $i--) { | |
// Get each number individually | |
$numbers[$i] = substr($num, $i - 1, 1); | |
// Multiply the number by the factor | |
$partial[$i] = $numbers[$i] * $factor; | |
// Sum of the digits | |
$sum += $partial[$i]; | |
if ($factor == $base) { | |
// Reset multiplication factor to 2 | |
$factor = 1; | |
} | |
$factor++; | |
} | |
// Calculation of modulo 11 | |
if ($r == 0) { | |
$sum *= 10; | |
$digit = $sum % 11; | |
if ($digit == 10) { | |
$digit = 0; | |
} | |
return $digit; | |
} elseif ($r == 1) { | |
$remainder = $sum % 11; | |
return $remainder; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment