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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to calculate a numeric value from alternating positions in a stri 2

Status
Not open for further replies.

bettyc

Technical User
Mar 6, 2009
8
US
Is there an easy way to perform the following calculation?

String value = "921030022447967916"
a) Each alternating character is multiplied by 2 or 1
(i.e. 9*2, 2*1, 1*2...)
b) All digits are then summed:
9*2=18 or 1+8 + 2*1 or 2 + 1*2 or 2...)

I am new to this website and any help with this is greatly appreciated.
 
Well, this is kind of fun. I wonder what it is for?

Create a formula like this:

stringvar x := {table.string};
numbervar i;
numbervar j := len(x);
numbervar y;

for i := 1 to j do(
if remainder(i,2) = 1 then
y := y + val(x)*2 else
y := y + val(x)
);
y

-LB
 
Thanks for the prompt response LB.

I still have a problem since I also need the following:
if the value of y > 9 then the calculation needs to be 10-y + 1. (All the digits are added as ones)

I tried to modify your formula but it's not working.

Thanks again...

Betty
 
What??? I guess I misread. I thought you wanted to add up the results of the first calculations, but now it appears you want to add the digits in each resulting number? As you might do in some numerology process? Given the above sample in your first post, what would the final result look like? Is it one number or is it a series of numbers?

-LB

 
Yes and yes...

The result of the sample shown is 81.

If you want more fun - the final part of this equation is to take the single digit from the result (the 1 - in the ones column) and deduct that from 10.

This would give me a "final" result of 9.

 
Do you ALWAYS deduct the value in the one's place from 10? If so:

stringvar x := {table.string};
numbervar i;
numbervar j := len(x);
stringvar array z;
numbervar p;
for i := 1 to j do(
redim preserve z;
if remainder(i,2) = 1 then
z := totext(val(x)*2,"00") else
z := totext(val(x),"00");
p := p + sum([val(z[1]),val(z[2])])
);
10 - val(right(totext(p,"0000"),1))

So now, what is the purpose of this exercise?

-LB
 
That was great, however, it only worked for the first record. It is part of a group footer and I need it to reset. How do I do that?

This is a formula used by banks for the "Check Digit" on a scan line for payment coupons. You know, the stubs that you send checks in with? This check digit verifies that the group of numbers preceding it in the line are correct.

Thank you!
 
I figured out which variable to reset and it works!

I (and my predessesor) have been trying to make this work in Crystal for a while now (well she had, I just started last week). I am absolutely a beginner when it comes to programming. She was able to create the formula in access but my client wants to transition the form into Crystal so here I am.

I was given this web site by another consultant that I would go to with Crystal questions and just found the info today. So happy I did!

I have to have 3 check digits in the scan line. Is the best way to use the same formula but changing the variable values? (i.e. x to w, p to q, etc.) I tried this for the 2nd check digit and it works.

Thank you again for your help. Hope you enjoyed working this out. I'm sure you'll be hearing from me again.


 
The formula, including resets, should have been:

stringvar x := {table.string};
numbervar i;
numbervar j := len(x);
stringvar array z := "";
numbervar p := 0;
for i := 1 to j do(
redim preserve z;
if remainder(i,2) = 1 then
z := totext(val(x)*2,"00") else
z := totext(val(x),"00");
p := p + sum([val(z[1]),val(z[2])])
);
10 - val(right(totext(p,"0000"),1))

I don't really follow what you mean by your last question about 3 check digits--or for that matter why the string is related to the final solution, so I don't know how to answer.

-LB
 
Sorry I was not too clear.

The string I provided was one of three strings that I need to calculate at the group footer.

Thanks.


~betty
 
Yes, you can just change the variable names.

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top