Precision error in JScript?

I am new to jscript and I have a problem.

I am writing a script to verify the IBAN bank account number in Belgium. I need to replace some letters by their position in the search string, and then I will convert this string to a number in order to run the modulo 97 test.

The first part goes well, but then with the conversion from string to number, 10 is added to my number. I do not know what I am doing wrong.

function checkIBAN() 
{
  var iban = crmForm.all.fp_iban.DataValue;

  if (iban != null)
  {
    iban = iban.substring(4) + iban.substring(0, 4);

    iban = iban.toUpperCase();
    var searchString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var pos;
    var tmp = '';

    for (x = 0; x < iban.length; x++) {
      pos = searchString.search(RegExp(iban.charAt(x),'i'));
      if (pos == -1)
        return false;
      else
        tmp += pos.toString();
    }

    alert(tmp); // Here my value is 735320036532111490

    var nr =parseInt(tmp);

    alert(nr); // Now my value seems to be 735320036532111500
    alert(nr % 97);   
    if (nr % 97 != 1)
    {
      alert('IBAN number is not correct !');
    }
  }
}
+3
source share
2 answers

Yes, 735320036532111490 is just too much to store in int. It will always be rounded:

alert(735320036532111490 / 10);
// alerts 73532003653211150

Here is a solution that might work for you .

+2
source

Source: https://habr.com/ru/post/1778062/


All Articles