Easiest way to get number of decimals

Posted on 2008-11-12
Last Modified: 2012-05-05
Can somebody show me the easiest way to get to know the number of decimals from a double

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

    Open in new window

    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)
    MessageBox.Show("No decimals")
    End If
    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

    Open in new window

    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".
    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?

    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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: jpaulino
    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 ( 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 ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now