In VBA is there an easy way to turn the decimal digits into an integer?

How would I store a number like 0.005 as 5 in an integer variable? I want to be able to take any decimal number, like 3.7, and store it as 7. Or 19.09 and store it as 9. What's the best way to do that in VBA?
shampouyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zorvek (Kevin Jones)ConsultantCommented:
Try this:

Fix((Value-Fix(Value))*(10^-(Fix(Application.LOG10(Value-Fix(Value)))-1)))

Kevin
0
Rodney EndrigaData AnalystCommented:
If you want just the last digit in the number, you can use:

{variable name} = Right(cell.Value, 1)

For instance, if Cell A1=3.7, it would result in 7.
For instance, if Cell A1=19.09 , it would result in 9.
0
zorvek (Kevin Jones)ConsultantCommented:
Alternatively, the first non-zero digit can be retrieved using this technique:

Public Sub Test()
    Debug.Print GetFirstFractionDigit(2.000042)
End Sub

Public Function GetFirstFractionDigit(ByVal Value As Double) As Long
    GetFirstFractionDigit = Left(CLng(Mid(CStr(Value), InStr(CStr(Value), ".") + 1)), 1)
End Function

Kevin
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

shampouyaAuthor Commented:
Would the first solution turn 4.082 into 82?
0
zorvek (Kevin Jones)ConsultantCommented:
Yes.
0
zorvek (Kevin Jones)ConsultantCommented:
But! You may see some anomalous digits appearing due to Excel's handling of reals. For example, 2.00004 might result in a 3 instead of a 4.

Kevin
0
zorvek (Kevin Jones)ConsultantCommented:
Correction, my first solution will NOT return additional digits.

This one will:

Public Sub Test()
    Debug.Print GetFirstFractionDigit(4.082)
End Sub

Public Function GetFirstFractionDigit(ByVal Value As Double) As Long
    GetFirstFractionDigit = CLng(Mid(CStr(Value), InStr(CStr(Value), ".") + 1))
End Function

Kevin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rodney EndrigaData AnalystCommented:
You can use something like this in your code:

result=Int(Right(cellValue, Len(cellValue) - InStr(cellValue, ".")))

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.