Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precision error in JScript?

I'm a jscript newbie and I've a problem.

I'm writing a script to validate an IBAN bank account number in Belgium. I need to replace some letters by their position in a searchstring and afterwards I convert this string into a number to take the modulo 97 test.

The first part goes well, but afterwards with the conversion from string to number, 10 is added to my number. I don't know what I'm 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 !');
    }
  }
}
like image 862
Robby Iven Avatar asked Jun 15 '26 14:06

Robby Iven


1 Answers

Yes, 735320036532111490 is simply too great a value to store in an int. It'll always be rounded:

alert(735320036532111490 / 10);
// alerts 73532003653211150

Here's a solution that might work for you.

like image 111
David Hedlund Avatar answered Jun 18 '26 03:06

David Hedlund