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

Thanks in advance
LVL 1
liversenAsked:
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.

DhaestCommented:
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)

Open in new window

0
MitzsCommented:
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
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
liversenAuthor 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

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

HooKooDooKuCommented:
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".
0
omegaomegaDeveloperCommented:
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
0
liversenAuthor 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
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
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.