How to find number of decimal places in Double
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: How to find number of decimal places in Double

  1. #1
    Join Date
    Aug 2007
    Posts
    28

    How to find number of decimal places in Double

    Hi,

    Please help me how to find number of decimal places in a double value.

    Example:
    I have two double variables

    double1 = 0.000000123450345454
    double2 = 0.0000001234

    i have to compare the above two doubles types and say that they are equal.

    I have to omit the digits after 0.0000001234 in double1 variable.

    Please help...


    Thanks in advance
    Last edited by sameer_thaj; January 28th, 2008 at 01:08 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    20

    Re: How to find number of decimal places in Double

    multiply the number by 10,000,000,000 then parse it to an int,, it will cut off the decimal places,, then u can compare or turn to double and compare.

    is the sneaky way,, but will work.

    cheers
    (.NET Version 2.0.50727) VS2005

  3. #3
    Join Date
    Aug 2007
    Posts
    28

    Re: How to find number of decimal places in Double

    Thaks for your reply,

    Using this solution i can compare only the above two variables.

    I may get any number of decimals as i showed above. I need a generic solution which can satisfy any number of decimals...


    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,981

    Re: How to find number of decimal places in Double

    Use the string length
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2007
    Posts
    28

    Re: How to find number of decimal places in Double

    When I convert the double to string, the contents is displayed in the Exponenet form. Using which i cannot find the length.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: How to find number of decimal places in Double

    // Are the two doubles very nearly the same?
    return Math.Abs(double1 - double2) < 0.0001;


    Use that, or something similar.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Aug 2007
    Posts
    28

    Re: How to find number of decimal places in Double

    Thanks for your support...


    I got the alternate


    I am using

    str = double1.ToString("F 30");
    str = str.TrimEnd('0');
    int Length = str.Length;

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: How to find number of decimal places in Double

    Thats an extremely inefficient way of testing whether two doubles are equal, and also probably horribly inaccurate.

    Code:
    Console.WriteLine("Are these equal?: ", (0.2 + 0.2 + 0.2 + 0.2 + 0.2) != 1);
    Funnily enough, this prints false. 0.2 can't be represented in binary exactly, therefore there's roundoff error. Your code would call these numbers not equal, because 0.9999999999 != 1.000000000. My code would call them equal.

    Comparing floating point numbers for equality is the worst thing you can do, it's impossible to do. The best option is making sure they're about the same.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to find number of decimal places in Double

    As Mutant_Fruit said, comparing floating point numbers is difficult (at best).

    Read this if you want to understand this very wide topic: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Laitinen

  10. #10
    Join Date
    Oct 2006
    Location
    Timisoara, Romania
    Posts
    123

    Re: How to find number of decimal places in Double

    .NET even provides an epsilon value to compare against: Double.Epsilon

  11. #11
    Join Date
    May 2007
    Posts
    1,546

    Re: How to find number of decimal places in Double

    Double.Epsilon is just the smallest value which can be represented in double precision. Adding 0.2 five times could easily result in a roundoff error greater than this. I wouldn't use it for comparisons.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  12. #12
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,981

    Re: How to find number of decimal places in Double

    Thought he'd realize that I meant to use the string length to figure out how much to adjust the amount by.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: How to find number of decimal places in Double

    Quote Originally Posted by Mutant_Fruit
    Double.Epsilon is just the smallest value which can be represented in double precision.
    NO, Double.Epsilon is the smallest value which can be represented between 0.0 and 1.0. (ie it is 0.0 + 1 LSB of Mantissa)

    This is an EXTREMELY important difference. As numbers get larger than 1.0 the increment between representable values becomes larger, and the potential for (absolute) error increases accordingly.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    May 2007
    Posts
    1,546

    Re: How to find number of decimal places in Double

    Quote Originally Posted by TheCPUWizard
    NO, Double.Epsilon is the smallest value which can be represented between 0.0 and 1.0. (ie it is 0.0 + 1 LSB of Mantissa)

    This is an EXTREMELY important difference. As numbers get larger than 1.0 the increment between representable values becomes larger, and the potential for (absolute) error increases accordingly.
    MSDN says:
    Represents the smallest positive Double value greater than zero
    I say:
    Double.Epsilon is just the smallest value which can be represented in double precision
    I don't see the difference here. I never said the Double.Epsilon is the increment between every floating point number.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  15. #15
    Join Date
    Jan 2008
    Posts
    20

    Smile Re: How to find number of decimal places in Double

    return Math.Abs(double1 - double2) < 0.0001;
    I agree with this method also, its probably the easiest and is accurate to whatever value you put after the <

    otherwise the method i suggested before with parsing it to an int combined with dglienna's string length idea (to get the shortest length of double, so you can trim the longer double to that length) would also work just fine but requires more code.

    I would go with the first method!
    (.NET Version 2.0.50727) VS2005

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center