VBA to identify last digit in integer

B

brucemc

I need to identify the last digit in a test integer.

I presently am about to convert the integer to a string, use
Right(TestInteger,1) to extract the last character, then convert that
character back to a value. Seems to me I, once again, am probably
missing some other painfully obvious and easier means.

Any ideas?
 
R

Ron Rosenfeld

I need to identify the last digit in a test integer.

I presently am about to convert the integer to a string, use
Right(TestInteger,1) to extract the last character, then convert that
character back to a value. Seems to me I, once again, am probably
missing some other painfully obvious and easier means.

Any ideas?

TestInteger Mod 10


This seems to work in VBA:

==========================
Sub LastDigit()
Const Num As Long = 79368412

Debug.Print "Last Digit in " & Num & " is " & _
Num Mod 10
End Sub

===========================


--ron
 
B

bgeier

Why convert to string, just test the numeric value itself

debug.print right(12345,1)
debug.print right(intValue,1)

either way works with out converting to a string
 
B

brucemc

Darn it, and Thank-YOU!

I've got to quit taking what MS writes as fact, unless there is a
"special" definition they were using for "String" in the description of
the RIGHT function...
 
B

bgeier

Microsoft uses "string" differently depending on what they are trying t
say.
In code "String" means a string of characters, regardless of how human
perceive the characters.

Whenever Microsoft uses "string" in a description of a function, the
mean whatever they want it to mean, usually it means any type o
character
 
G

Guest

The solution to your specific problem has been identified already.

This is an interesting problem. The number of digits in the integer part of
any number is given by:

int(1+log(YourNumber)/log(10))

where YourNumber can be an integer or float. Say YourNumber is 1236.45, the
last digit is:

Debug.Print mid(1236.45,int(1+log(1236.45)/log(10)),1)

This can be extended; say, you wanted the last but one digit, the position is:

int(1+log(YourNumber)/log(10)) -1

etc..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top