Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to write a modulus 10 checking ?

280 views
Skip to first unread message

Philippe Ranger

unread,
Mar 30, 1999, 3:00:00 AM3/30/99
to
<<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;
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

Roelof Engelbrecht

unread,
Mar 30, 1999, 3:00:00 AM3/30/99
to
Klint <kh...@dbix.com.my> wrote in message
news:01be7ba8$7b2cac40$0d00...@ngpc.dbix.com.my...

> is there a mod 10 function in delphi ?

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...


Klint

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to
is there a mod 10 function in delphi ?


thanks

Wayne Herbert

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to
Nope... don't think that is it... if he is talking check digits.
Standard mod 10 check digits use a weight of 137. You take a
number... say 456789 and multply by 137137 as follows

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

Philippe Ranger

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to
<<Wayne:

Nope... don't think that is it... if he is talking check digits.
Standard mod 10 check digits use a weight of 137. You take a
number... say 456789 and multply by 137137 as follows
>>

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


MendriX

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to
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.

-----------

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)


Wayne Herbert

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to
This works for a check on an already existing number. For generating
nwe numbers you would have to make sure that Total mod 11 not equal to
10... one reason some folks don't like mod 11... you lose account
numbers.

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);

--

Wayne Herbert

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to
Thanks, Philippe. I made one error. The check digit should be computed as 10
minus the mod 10. Thus, your last line should read:

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 Ackbar

unread,
Apr 12, 1999, 3:00:00 AM4/12/99
to
Well, no function will work with numbers that large. If you want something
with arbitrarily large numbers, you'll have to use some other libraries of
commands. The one I'm most familiar with it the Java BigInteger class, and
the Advance Placement case study on the BigInt class, which uses a character
array to store the digits in reverse order.

--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

James

unread,
Apr 13, 1999, 3:00:00 AM4/13/99
to
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;

Philippe Ranger

unread,
Apr 13, 1999, 3:00:00 AM4/13/99
to
<<James:

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
>>

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

James

unread,
Apr 16, 1999, 3:00:00 AM4/16/99
to
What and where is Wayne's algorithm, dear sir ?

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>...
>...

Philippe Ranger

unread,
Apr 16, 1999, 3:00:00 AM4/16/99
to
<<James:

What and where is Wayne's algorithm, dear sir ?
>>

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;


0 new messages