I am trying to understand why the modulo operation does not work as expected :
I need to validate an IBAN, and the algoritthm includes doing a modulo.
According to Wikipedia : enter link description here
3214282912345698765432161182 mod 97 = 1
According to my Windows calculator :: 3214282912345698765432161182 mod 97 = 1
BUT when I do it in JS I get 65 :
var result = 3214282912345698765432161182 % 97;
console.log(result);
// result is 65
Why am I getting 65 and not 1 in JS?
Your number is higher than (2^53)-1 thus it won't be correctly represented.
You can use the function Number#isSafeInteger
to check if it is safe to use this number
const isSafe = Number.isSafeInteger(3214282912345698765432161182);
console.log(isSafe);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With