tags:

views:

49

answers:

1
+1  Q: 

Luhn check digit

I cant seem to figure out what is wrong with my check digit code!

At times, it produces 2 length check digit values

Example

1277531815000110 <-- check digit is double value??????
1277532495000110 <-- check digit is double value???????
1277534649000110 <-- check digit is double value???????
127753185300011 <-- good!
127753208500019 <-- good!

All generated numbers are valid, it can be checked at http://www.ee.unb.ca/cgi-bin/tervo/luhn.pl?N=127753224800013

CODE: http://tinyurl.com/352acpj

+1  A: 

This line's the problem:

CheckSumNumber = (((sum / 10) + 1) * 10) - sum;

That will generate 10 when sum is already a multiple of 10. Basically you're just trying to round up. Here's an easy way of doing it:

CheckSumNumber = (((sum + 9) / 10) * 10) - sum;
Jon Skeet
Just beat me to it!
Oded