Easiest way to get number of decimals

Can somebody show me the easiest way to get to know the number of decimals from a double

Example:
0.1     I need to have the number 1 returned
0.06     I need to have the number 2 returned
0.004    I need to have the number 3 returned

LVL 1
Who is Participating?
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.

Project managerCommented:
Hmmm;... also interested .
I did it this way:

``````Dim val As String = System.Text.RegularExpressions.Regex.Replace((numToRound Mod 1).ToString(), "0*\$", "")

MessageBox.Show("numbers of decimals: " + (val.Length()-2)
``````
Commented:
another method that produce the same result and will also handle where no. is not decimal.

Dim x As Decimal = 100.5
Dim y As Integer
y = x.ToString.IndexOf(".")
If y > 0 Then
MessageBox.Show("number of chars : " & x.ToString.Substring(y + 1).Length.ToString)
Else
MessageBox.Show("No decimals")
End If

Experts Exchange Solution brought to you by

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

Author Commented:
Hmmm. I can't use the solution, but it is my own fault. I made a bad example
the values I have can also be like this:

34.01     Should return 2
345.003 should return 3
2343432.1234 should return 4

Meanwhile i have this idea: (See attached code)

Is there still a better way?

``````Dim strStepSize as string= dblStepSize
Dim NoOfDecimals As String = _
strStepSize.Substring(strStepSize.IndexOf(".")).Length - 1
``````
Commented:
Your proposed solution looks very elegant.  But depending upon what you are attempting to accomplish, I find the whole thing to be sort of problomatic.  Because when you are dealing with floating point numbers, there will be so many instances where the value stored in a double is going to be something like"1.233999999999998".
DeveloperCommented:
Hello, liversen,

Like HooKooDooKu says...   So I wonder about two things and maybe your answers will clarify things for us.
1. Why do you want to do this?  (I.e. what is the underlying objective?)
2. Why can't you use Mitzs' solution?

Cheers,
Randy
Author Commented:

ad1: It's complicated. I need it for the Transact SQL rounding function which has a truncate parameter I need to set.

ad2: I can use Mitzs' solution. It was the solution from Dhaest: that I can not use.  Mitz just wrote his answer right before me and I did not see it at that time.

I'll go ahead and award the points to Mitzs

Thanks everyone
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
Visual Basic.NET

From novice to tech pro — start learning today.