If you mean the trick whereby you add all the digits in a number,
recursively, until you get a single digit, this ought to do it —
Function mod10 (n: integer): integer;
Var
s: string;
j: integer;
Begin
if (n<0) then n := -n;
repeat
s := intToStr(n);
n := 0;
for j := 1 to length(s) do inc(n, ord(s[j]) - ord('0'));
until (n < 10);
result := n;
End;
PhR
From the Help file (look in the index for "mod"):
The mod operator returns the remainder obtained by dividing its operands. In
other words, x mod y = x - (x div y) * y.
IOW:
ModValue := X mod 10;
is what you need.
Roelof
------
remove the obvious from my reply address...
thanks
9*7 = 63
8*3 = 24
7*1 = 07
6*7 = 42
5*3 = 15
4*1 = 04
Add up the totals and take mod 10 = (155 mod 10) = 5. Therefore the
check digit for 456789 is 5.
Mod 11 is better at preventing errors, though.
Philippe Ranger wrote:
> <<Klint:
> is there a mod 10 function in delphi ?
> >>
>
> If you mean the trick whereby you add all the digits in a number,
> recursively, until you get a single digit, this ought to do it —
--
Wayne Herbert
Manager, Computer Products
Key Maps, Inc.
1411 West Alabama
Houston, TX 77006
Vox: 713.522.7949
Fax: 713.521.3202
Email: wher...@keymaps.com
"How can we POSSIBLY use sex to get what we want? Sex IS what we
want!"
--Dr. Frasier Crane
So, YOU know what mod 10 checking means. I didn't. And it's a fact that the
thing I suggested is indifferent to digit order, so it's a very poor mistype
check. The code for your formula would be —
Function mod10 (n: integer): integer;
Const
CaWeights: array[0..2] of integer = (7, 1, 3);
Var
s: string;
j, tot, digw: integer;
Begin
if (n<0) then n := -n;
s := intToStr(n);
tot := 0;
for j := 1 to length(s) do begin
digw := ord(s[j]) - ord('0');
digw := digw * CaWeights[j mod (1 + high(CaWeights))];
inc(tot, digw);
end;
result := tot mod 10;
End;
PhR
>Mod 11 is better at preventing errors, though.
A few days ago I wrote a function to check (Dutch) bankaccount
numbers, and Dutch soc. sec. numbers (/tax-numbers) which are coded
conform a mod 11 algorithm.
-----------
function Check11(const S: String): Boolean;
//True, if given S (representing a value) is conform the 11-check
//algorithm (characters . , - / \ and space, are ignored)
//False, also if S is not a number
//Usage: Dutch SOFI numbers are conform the '11-proef'
var
AbsIndex, //index of CHARACTER, counted from the left
DigitIndex, //how many-th DIGIT counted from the right
CheckDigit,Total : Cardinal;
Value : Byte;
begin
Result := False; //default: not conform algorithm
Total := 0;
CheckDigit := 0;
DigitIndex := 1; //1 is pos. of most right digit, which is check
digit
for AbsIndex := Length(S) downto 1 do begin //start at most right
character
if S[AbsIndex] in ['.',',','-','/','\'] then //ignore these
characters
Continue;
if not (S[AbsIndex] in ['0'..'9']) then //illegal character,
return False
Exit;
Value := Ord(S[AbsIndex])-Ord('0'); //what's the digit's value
if DigitIndex=1 then //most right digit, is check digit
CheckDigit := Value //store check digit
else
Inc(Total,Value*DigitIndex);
Inc(DigitIndex);
end;
Result := (DigitIndex>1) and ((Total mod 11)=CheckDigit);
end;
-----------
---
MarK <men...@dds.PLEASE.REMOVE.THISnl> (PGP-id: 0x4ED01C6D)
MendriX wrote:
> On Wed, 31 Mar 1999 09:45:03 -0600, Wayne Herbert
> <wher...@keymaps.com> wrote:
>
> >Mod 11 is better at preventing errors, though.
>
> A few days ago I wrote a function to check (Dutch) bankaccount
> numbers, and Dutch soc. sec. numbers (/tax-numbers) which are coded
> conform a mod 11 algorithm.
>
> Result := (DigitIndex>1) and ((Total mod 11)=CheckDigit);
--
Result := 10 - (tot mod 10);
Also, if anyone is really serious about using this make sure the multiplier is
137137 and not 731731 (run a couple by hand)... its been such a long time :>)
Philippe Ranger wrote:
> So, YOU know what mod 10 checking means. I didn't. And it's a fact that the
> thing I suggested is indifferent to digit order, so it's a very poor mistype
> check. The code for your formula would be —
>
> Function mod10 (n: integer): integer;
> Const
> CaWeights: array[0..2] of integer = (7, 1, 3);
> Var
> s: string;
> j, tot, digw: integer;
> Begin
> if (n<0) then n := -n;
> s := intToStr(n);
> tot := 0;
> for j := 1 to length(s) do begin
> digw := ord(s[j]) - ord('0');
> digw := digw * CaWeights[j mod (1 + high(CaWeights))];
> inc(tot, digw);
> end;
> result := tot mod 10;
> End;
>
> PhR
--
--Joe
James <ja...@hotmail.com> wrote in message
news:01be863d$27ce2160$0d00...@ngpc.dbix.com.my...
> yeah. your function works but if the number is very big with 20 digits
then
> the function cannot accept the 20 digits as it can only accept integers
Philippe Ranger <.> wrote in article <7ds426$ap...@forums.borland.com>...
> <<Klint:
> is there a mod 10 function in delphi ?
> >>
>
> If you mean the trick whereby you add all the digits in a number,
> recursively, until you get a single digit, this ought to do it —
>
> Function mod10 (n: integer): integer;
> Var
> s: string;
> j: integer;
> Begin
> if (n<0) then n := -n;
You're posting in a thread that's been dead nearly two weeks. I wasn't
answering the right question anyhow. Wayne has the right algorithm. If you
have a number beyond integer size, then likely you have it as a string of
digits to begin with. So it doesn't need conversion from integer, and
Wayne's algorithm will work straight on the digits, any number.
PhR
I am looking forward to this generic function that can do a mod 10 checking
?
Philippe Ranger <.> wrote in article <7f0el6$en...@forums.borland.com>...
>...
You revived a two-week-dead thread. It's at the beginning. Anyhow, here's my
code for it, but mod10 checking is a standard rule, and it uses a digit
series. The code below uses 137137..., despite how it looks. Wayne is not
sure if the series is that or the reverse, 731731... So, if you have some
mod10-checked values, verify.
PhR
------------
Function mod10 (n: integer): integer;
Const
CaWeights: array[0..2] of integer = (7, 1, 3);
Var
s: string;
j, tot, digw: integer;
Begin
if (n<0) then n := -n;
s := intToStr(n);
tot := 0;
for j := 1 to length(s) do begin
digw := ord(s[j]) - ord('0');
digw := digw * CaWeights[j mod (1 + high(CaWeights))];
inc(tot, digw);
end;
result := 10 - (tot mod 10);
End;