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!

calculating numeric values from a predefined string position 1

Status
Not open for further replies.

bchang88

Instructor
Mar 6, 2009
3
US
This is a continuation of a previous thread767-1535083.

My original thread is calculating the below @Scanline by:
String value = "6246300224479"
a) Each alternating character is multiplied by 2 and 1
(i.e. 6*2, 2*1, 4*2...)
b) All digits are then summed:
6*2=12 or 1+2 + 2*1 or 2 + 4*2 or 8...)
c) The sum of the above value = 59
d) The sum is divided by 10 and the remainder of 9 is deducted from 10 leaving a final value of 1.

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])]));
totext(abs(10 - val(right(totext(p,"0000"),1))),"0")

The above works great!
BUT, now I need a different group of multipliers.
The new alternating multiplier group is 7,5,3,2...
String value = "6246300224479"
a) Each alternating character is multiplied by 7,5,3 and 2
(i.e. 6*7, 2*5, 4*3...)
b) All digits are then summed:
(6*7=42 or 4+2) + (2*5=10 or 1+0) + (4*3=6 or 6) + (6*2=12 or 1+2) + (0*7=0 or 0) + (0*5=0 or 0)+...
c) The sum of the above value = 44
d) The sum is divided by 10 and the remainder of 4 is deducted from 10 leaving a final value of 6.
(if the remainder is 0, then the final value is 0)

Any help I can get is REALLY APPRECIATED!!!
 
If you come up with a solution to this please indicate so .. I have been playing with it in my spare time (limited lately) and do not want to continue if it isn't needed any longer..

Thanks

_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
nevermind I was further into it than i thought

local stringvar b := {table.string};
local numbervar a := len(b);
local numbervar d;
local numbervar e;
local numbervar f;
local numbervar numberout;
local numbervar Array multiplier := [7,5,3,2];
for f := 1 to 4 do
(
for d := f to a step 4 do //
(
e := tonumber(mid(b,d,1)) * multiplier[f];
if e > 9 then e := int(e/10)+ remainder(e,10);
numberout := numberout + e;
);
);
numberout

_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
I just read the previous post you referred to above... Im glad I didnt read it first so I could come up with a solution on my own because I agree with LBass... it was kind of fun..
However, she may have a better solution than mine ... hopefully she will have some time to chime in...

_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
I forgot instruction d..

add the following lines before the last line .. so the last four lines should read

);
if remainder(numberout,10) <> 0 then numberout := 10-remainder(numberout,10)
else numberout := 0;
numberout


_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
@CoSpringsGuy
I'm sorry I didn't respond earlier, I didn't get an email notification that someone had posted on this thread. I am going to try your formula over the weekend and will advise. Thank you for taking the time to work it out!


 
@CoSpringsGuy

Finally got a chance to implement and test the formula and it works GREAT!!! Thank you so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top