Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modulus 10

Status
Not open for further replies.

ErnstNoto

Programmer
Dec 20, 2009
48
SE
Hi!
I need to calculate modulus 10 in a crystal reports

I know how to calculate this manually but I have a bit of a problem figuring out how to calculate the check digit and create the new number in crystal reports.

Anyone who can help?

Best Regards
Ernst

Best Regards
Ernst Noto
 

This seems to work, but please test thoroughly:

Code:
whileprintingrecords;
stringvar v_cc;
stringvar v_temp;
numbervar v_calc;
numbervar v_result;
numbervar v_counter;

v_cc := replace({YourCCfield}," ","");

// Double every other digit starting with the next to last digit.

numbervar v_counter := len(v_cc) - 1;

while v_counter > 0

do
(v_temp := mid(v_cc,v_counter,1);
v_calc := tonumber(v_temp) * 2;

if v_calc >= 10 then v_calc := tonumber(left(totext(v_calc,"#",0),1))+ tonumber(right(totext(v_calc,"#",0),1))
else
v_calc;

v_result := v_result + v_calc;
v_counter := v_counter - 2);

// Add in the non-doubled digits
v_counter := len(v_cc);

while v_counter > 0

do
(v_temp := mid(v_cc,v_counter,1);
v_calc := tonumber(v_temp);
if v_calc >= 10 then v_calc := tonumber(left(totext(v_calc,"#",0),1)) + tonumber(right(totext(v_calc,"#",0),1))
else
v_calc;

v_result := v_result + v_calc;
v_counter := v_counter - 2);

// Calculate check digit
v_result mod 10

 
Thank you!
This I would not have figured out, thank you. I think we're on the right track. I have tried it and the formula works and does calculate a digit, but it is not the correct one.

Ex: I have this number: 2238310098314000000
The check digit with moudulus10 should be 1 and my new number (CID) should then become: 22383100983140000001

But when using your formula, it returns the digit 6 and the new number then becomes 22383100983140000006

In your formula you have noted the following: // Double every other digit starting with the next to last digit.

But should you not start doubling the last digit and not the next to last? I am however not sure if this is the only thing, I cant say cause I dont know if I understand the formula.

Best Regards
Ernst

Best Regards
Ernst Noto
 

It seemed like there was conflicting information on how to do this on the sites I looked at.

Please describe your method for calculating this manually and we can adjust the formula accordingly.

 

OK, this works better:

Code:
whileprintingrecords;
stringvar v_cc;
stringvar v_temp;
numbervar v_calc;
numbervar v_result;
numbervar v_counter;

v_cc := replace({YourDBField}," ","");

// Double every other digit starting with the last digit.

numbervar v_counter := len(v_cc);

while v_counter > 0

do
(v_temp := mid(v_cc,v_counter,1);
v_calc := tonumber(v_temp) * 2;

if v_calc >= 10 then v_calc := tonumber(left(totext(v_calc,"#",0),1))+ tonumber(right(totext(v_calc,"#",0),1))
else
v_calc;

v_result := v_result + v_calc;
v_counter := v_counter - 2);

// Add in the non-doubled digits
v_counter := len(v_cc) - 1;

while v_counter > 0

do
(v_temp := mid(v_cc,v_counter,1);
v_calc := tonumber(v_temp);
if v_calc >= 10 then v_calc := tonumber(left(totext(v_calc,"#",0),1)) + tonumber(right(totext(v_calc,"#",0),1))
else
v_calc;

v_result := v_result + v_calc;
v_counter := v_counter - 2);

// Calculate check digit
v_cc + right(totext(v_result * 9,"#",0),1)

 
Hi!
Thank you again, but I am afraid it still isnt correct. That is, I find that the first number the report encounters, is alway correct, and the rest is always wrong.

I dont know what happens, but the formula does not seem to reset when it is going to calculate the next number it encounter. If I only show one number it is correct and otherwise, only the first number.

Best Regard
Ernst





Best Regards
Ernst Noto
 

Sorry, I was only testing one number at a time.

Change the variable declarations at the beginning of the formula to reset for every record:

whileprintingrecords;
stringvar v_cc := "";
stringvar v_temp := "";
numbervar v_calc := 0;
numbervar v_result := 0;
numbervar v_counter := 0;

That should do the trick.

 
Brilliant!

Thank you

Best Regards
Ernst

Best Regards
Ernst Noto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top