0

I'm revisiting an old classic ASP application to add some new requisites, one of them needs to perform an aritmethic operation to generate an id according to some standard rule for bank files which:

  • Generates a number from a long string according to some rules
  • Gets mod of dividing this long number by 97
  • Substracts this result to 98
  • And preppends a 0 if result is only one digit

The number I get as result of first operation is 1185626240142800 (many many magnitudes over max long number), I can convert to Double but then I can't get mod 97.

This rule is a standard for bank files so there is no workaround, I need to perform these operations.

In my Windows 7 64 bits I saw I can perform this operation in SQL Server, so as a bad hack it's not a bad idea, any other ideas?

BTW, this app will run on a Windows server 2003 32 bits, in case you think this will be an added problem

K. Weber
  • 2,643
  • 5
  • 45
  • 77
  • 1
    See if this pseudocode helps: http://bytes.com/topic/software-development/insights/793965-how-find-modulus-very-large-number – Salman A Jun 11 '14 at 10:38
  • 1
    Answers are in JavaScript but it is easy to convert them to vbscript: http://stackoverflow.com/questions/929910/modulo-in-javascript-large-number – Salman A Jun 11 '14 at 10:44

1 Answers1

1

Numbers larger than 2,147,483,647 cannot fit inside the Long datatype. You can implement the algorithm described in this article:

Pseudo code:

To find nBig % a:

Let d = the number of digits that can be processed at a time.

Repeat while val( nBig ) >= a
1. tmpStr := d digits from the LHS of nBig.
2. nBig := Remaining portion of nBig
3. tmpNum := toInteger( tmpStr )
4. tmpNum := tmpNum % a
5. tmpStr := toString( tmpNum )
6. nBig := tmpStr + nBig

Selecting d:

d should satisfy following constraint

max no. of digits in Mod a < d <= max. digits in tmpNum - max no. of digits in Mod a
(For eg: If a = 512 (Mod ranges 0..511), and tmpNum can store max 16 digits, then 3 < d <= 16-3)
(For eg: If a = 100 (Mod ranges 0..99), and tmpNum can store max 16 digits, then 2 < d <= 16-2)

VBScript/VBA Code:

Function LongMod(strDividend As String, numDivisor As Long) As Long

    Dim d As Long
    Dim strTemp As String
    Dim numTemp As Long

    d = 9 - Len(CStr(numDivisor - 1))

    While CDbl(strDividend) >= numDivisor
        strTemp = Left(strDividend, d)
        numTemp = CLng(strTemp) Mod numDivisor
        strDividend = CStr(numTemp) & Mid(strDividend, d + 1)
    Wend

    LongMod = CLng(strDividend)

End Function

LongMod("1185626240142800",               97) ' 21
LongMod("330542811101000000123456182900", 97) ' 38
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 1
    The `Dim [var] As [type]` syntax isn't valid VBScript. I *think* this code will work in VBScript if you simply lose the "`As Long`" and "`As String`" parts (from everywhere, including the function declaration), but I haven't tested it. – Martha Jun 13 '14 at 16:12
  • @Martha: correct. The `AS [type]` portions work in VBA and not in VBScript. I've choose to add them so that people can understand what datatype the variables are. – Salman A Jun 14 '14 at 06:13