
January 28th, 2008, 01:04 AM
#1
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.

January 28th, 2008, 01:14 AM
#2
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

January 28th, 2008, 01:19 AM
#3
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

January 28th, 2008, 01:33 AM
#4
Re: How to find number of decimal places in Double

January 28th, 2008, 02:44 AM
#5
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.

January 28th, 2008, 03:17 AM
#6
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.

January 28th, 2008, 06:30 AM
#7
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;

January 28th, 2008, 07:00 AM
#8
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.

January 28th, 2008, 07:56 AM
#9
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 FloatingPoint Arithmetic
Laitinen

January 28th, 2008, 08:53 AM
#10
Re: How to find number of decimal places in Double
.NET even provides an epsilon value to compare against: Double.Epsilon

January 28th, 2008, 12:51 PM
#11
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.

January 28th, 2008, 12:55 PM
#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.

January 28th, 2008, 01:26 PM
#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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; 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

January 28th, 2008, 02:49 PM
#14
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.
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.

January 28th, 2008, 04:22 PM
#15
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

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
