C# How to calculate first 10 numbers out a bill number of 12?
Lets assume i have a bill number that has 12 numbers: 823 45678912
My question is h开发者_如何转开发ow exactly do i do calculations with the first 10 numbers?
To verify if a given in billnumber is correct i have to perform the following calculation:
(first 10 numbers) % 97 = result
And if the result of the calculation is the same as the last 2 numbers of my bill number, then it is verified.
Thanks in advance.
(n / 100) % 97 == n % 100
It looks like you have a bill number that is actually a string with spaces. I would use @Marcelo's solution, but first you'll need to convert it to a long integer. This should help with that.
var billAsNumber = long.Parse( billNumber.Replace(" ","") );
var valid = (billAsNumber / 100) % 97 == billAsNumber % 100;
If your bill number is stored in a long
variable, shift it right two places and there you have it:
var first10Digits = billNumber / 100;
var checksum = first10Digits % 97;
Note that your bill number might have more than 12 digits and the resulting integer would not fit into an existing data type. While searching for solutions on how to do MOD 97 division on very large numbers i've found this algorithm that worked well for me.
精彩评论