CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: How to find number of decimal places in Double

1. Member
Join Date
Aug 2007
Posts
28

## How to find number of decimal places in Double

Hi,

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.

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

2. Junior Member
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

3. Member
Join Date
Aug 2007
Posts
28

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

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. ## Re: How to find number of decimal places in Double

Use the string length

5. Member
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. Senior Member
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.

7. Member
Join Date
Aug 2007
Posts
28

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

I got the alternate

I am using

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

8. Senior Member
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.

9. Senior Member
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. ## Re: How to find number of decimal places in Double

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

11. Senior Member
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.

12. ## 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.

13. ## Re: How to find number of decimal places in Double

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.

14. Senior Member
Join Date
May 2007
Posts
1,546

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

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.

15. Junior Member
Join Date
Jan 2008
Posts
20

## 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!

#### Posting Permissions

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