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!

Simple Mathematical Question 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi,

I need to find the last digit of a 10-digit integer. I have tried to use Mod 10 but the number seems to be too large to use with the mod operator. Has anyone got any ideas?

:)
 
What about converting it to a string and taking the last digit that way?

NumString := inttoStr(num);
lastdigit := numString[length(numstring)];

lastDigitStr := strToInt(lastDigit);

There's probably a more elegant way of doing this...

lou
 
Thanks for the suggestion but the number is lost in the first IntToStr conversion and NumString ends up equalling: -65531 !!!
 
Sorry, yes, doesn't work.

The number 4294901765 exceeds LongInt range. Int64 type may be required but I tried my suggestion and it doesn't work with that either.

lou
 
hi

This worked for the few example numbers I tried :

procedure TForm1.Button1Click(Sender: TObject);
var num : int64;
s : string;
numb : integer;
begin
num := 4294901765;
numb := num mod 10;

s := inttostr(numb);

label1.Caption := s;
end;

...give a bit more testing...

lou
 
Let me try and explain in a better way than I have already!!

A function I use to get the Paradox version of a specified database table returns a LongInt variable called table_level containing the version number. However, the only way to view this number seems to be by using the following line:

showMessageFmt('%s is a Level %u table.', [table.TableName, table_level]);

Any other method of outputting gives a table_level value of -65531. So I guess by using %u, I am casting to an unsigned integer?? So i have tried assigning table_level to Cardinal, LongWord and other type of integers but to know avail. The original outputted number (4294901765) seems to disappear!

Can anyone help?!
 
Woohoo! I've solved it now. The only way to get at the number being outputted by the ShowMessageFmt box was to use the Format function and store the result in a string variable. I then used Lou's idea of grabbing the last character of the string before converting it back to a normal integer! Thanks for your help Lou - much appreciated :)
 
Wouldn't...

showMessageFmt('%s is a Level %u table.',
[table.TableName, (table_level mod 10)]);

...have done the same thing?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top