Solved

# Easiest way to get number of decimals

Posted on 2008-11-12
223 Views
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

0
Question by:liversen

LVL 53

Expert Comment

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)
``````
0

LVL 4

Accepted Solution

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

LVL 1

Author Comment

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
``````
0

LVL 16

Expert Comment

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

LVL 12

Expert Comment

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

LVL 1

Author Comment

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

## Featured Post

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…