# 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?
###### Who is Participating?

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.

ConsultantCommented:
Try this:

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

Kevin
0
Data 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
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
Author Commented:
Would the first solution turn 4.082 into 82?
0
ConsultantCommented:
Yes.
0
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
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

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