miltonrodrigueza
asked on
23 digit number in numeric variable
Hi,
I need to store a 23 digit number in a 'numeric' variable. e.g. 20041010050500013402606
At the moment i have the string stored in a 'String' variable, However i need to convert it to a numeric type variable in order to apply a modulus 97.
I have tried with an integer variable using StrToInt(), I have tried with an Int64 variable using StrToInt64() but unfortunately it always come back with a compilation error "Integer constant too long". If i continue the execution of the program the the system throws an error message that reads "20041010050500013402606 is not a valid integer value"
I am currently using Delphi 6 enterprise edition.
I appreciate any help you can provide me with on this matter.
Cheers.
I need to store a 23 digit number in a 'numeric' variable. e.g. 20041010050500013402606
At the moment i have the string stored in a 'String' variable, However i need to convert it to a numeric type variable in order to apply a modulus 97.
I have tried with an integer variable using StrToInt(), I have tried with an Int64 variable using StrToInt64() but unfortunately it always come back with a compilation error "Integer constant too long". If i continue the execution of the program the the system throws an error message that reads "20041010050500013402606 is not a valid integer value"
I am currently using Delphi 6 enterprise edition.
I appreciate any help you can provide me with on this matter.
Cheers.
Int64 is insufficient for your needs as it ranges from
-9223372036854775808 .. 9223372036854775807
i.e. max of 19 digits, you need 23
Regards
Pierre
-9223372036854775808 .. 9223372036854775807
i.e. max of 19 digits, you need 23
Regards
Pierre
ASKER
Hi Mokule,
I need to apply a Modulus 97 to that value and as far as i understand the function MOD requires 2 integer input parameters so i do not know how to do that calculation with a string parameter, could you please instruct me how to do that?
Many thanks.
I need to apply a Modulus 97 to that value and as far as i understand the function MOD requires 2 integer input parameters so i do not know how to do that calculation with a string parameter, could you please instruct me how to do that?
Many thanks.
ASKER
Hi PierreC,
Thanks for the info. Do you have any ideas how can i work around this issue... maybe a user defined type or something like that.
Regards,
Thanks for the info. Do you have any ideas how can i work around this issue... maybe a user defined type or something like that.
Regards,
for a string library on big integers, see my post with the code from here: https://www.experts-exchange.com/questions/21812621/How-can-I-Increase-the-accurate-of-floating-point-multiplying.html
get the two highest order digits convert to integer.
if it is greater or equal 97 subtract 97 from it
The difference multiply by 10 add next digit and continue.
if it is greater or equal 97 subtract 97 from it
The difference multiply by 10 add next digit and continue.
Search for representation of large integers in Pascal, you will find many implementaion
Hi, I think this is what you are looking for
http://delphiforfun.org/Programs/Library/big_integers.htm
Operations supported are: Assign, Add, Subtract, Multiply, Divide, Modulo, Compare, Factorial, and ConvertToDecimalString.
It will support integers far larger than your needs
David
http://delphiforfun.org/Programs/Library/big_integers.htm
Operations supported are: Assign, Add, Subtract, Multiply, Divide, Modulo, Compare, Factorial, and ConvertToDecimalString.
It will support integers far larger than your needs
David
I was just about to post the above link :)
By no means it's efficient.
But is it worth to install those libraries for a few lines of code?
function Modulo97(s: string): integer;
var
val: integer;
i: integer;
begin
val := 0;
if Length(s) > 0 then
begin
val := Ord(s[1])-Ord('0');
i := 2;
while i <= Length(s) do
begin
val := 10*val+Ord(s[i])-Ord('0');
val := val mod 97;
Inc(i);
end;
end;
Result := val;
end;
But is it worth to install those libraries for a few lines of code?
function Modulo97(s: string): integer;
var
val: integer;
i: integer;
begin
val := 0;
if Length(s) > 0 then
begin
val := Ord(s[1])-Ord('0');
i := 2;
while i <= Length(s) do
begin
val := 10*val+Ord(s[i])-Ord('0');
val := val mod 97;
Inc(i);
end;
end;
Result := val;
end;
A little bit simpler version.
I've assumed it's positive and I don't check for valid chars.
function Modulo97(s: string): integer;
var
val: integer;
i: integer;
begin
val := 0;
if Length(s) > 0 then
begin
i := 1;
while i <= Length(s) do
begin
val := 10*val+Ord(s[i])-Ord('0');
val := val mod 97;
Inc(i);
end;
end;
Result := val;
end;
I've assumed it's positive and I don't check for valid chars.
function Modulo97(s: string): integer;
var
val: integer;
i: integer;
begin
val := 0;
if Length(s) > 0 then
begin
i := 1;
while i <= Length(s) do
begin
val := 10*val+Ord(s[i])-Ord('0');
val := val mod 97;
Inc(i);
end;
end;
Result := val;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>But is it worth to install those libraries for a few lines of code?
there is nothing to install for my code, for example (I didn't look at the others) It's just a copy paste in a unit and that's that :)
there is nothing to install for my code, for example (I didn't look at the others) It's just a copy paste in a unit and that's that :)
> ciuly
sorry to be awkward :(
I appreciate a lot of work, but Your solution makes no real sens.
It's so slow You can't calculate modulo on 9 digit numbers. not speaking of 23.
sorry to be awkward :(
I appreciate a lot of work, but Your solution makes no real sens.
It's so slow You can't calculate modulo on 9 digit numbers. not speaking of 23.
hm... you have a point here :) I actually designed it for big numbers (a AND b being big numbers) in which case it works pretty good.
well, my bad. never mind my code, it's not for this particular issue :D
well, my bad. never mind my code, it's not for this particular issue :D
do the calculation on a string data