Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modulo not working in Javascript

Tags:

javascript

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?

like image 533
bretondev Avatar asked Mar 07 '23 13:03

bretondev


1 Answers

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);
like image 193
Weedoze Avatar answered Mar 24 '23 05:03

Weedoze