0

Again i come back with one of my problems and queries.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

namespace compare_string
   {
     class Program
     {
        static void Main(string[] args)
        {
            string str1 = "85.8500000000000";
            string str2 = "85.85";
            double num1 = Convert.ToDouble(str1);
            double num2 = Convert.ToDouble(str2);
            if (num1 != num2)
            {
                 Console.WriteLine("Unequal");
            }
            else {
                 Console.WriteLine("Equal");
            }
             Console.ReadKey();
         }
    }
  }

Why is give that the two numbers are unequal? Thanks in advance!

amigo
  • 155
  • 1
  • 10
  • 3
    They show as equal when I run this code – Steve Danner Nov 20 '12 at 13:18
  • @κωστας Σοφός Are you sure, is the "." your system decimal separator? – Hamlet Hakobyan Nov 20 '12 at 13:22
  • 1
    also, try to avoid comparations between floating point numbers, since they tend to be a problem usually, see here: http://stackoverflow.com/questions/1398753/comparing-double-values-in-c-sharp – fnurglewitz Nov 20 '12 at 13:25
  • @tr3 What's the use of numbers if you're not allowed to compare them? Do you know of an application? – Jeppe Stig Nielsen Nov 20 '12 at 13:32
  • 1
    @JeppeStigNielsen I believe he meant to compare *equality* between floats. It's better to see if the difference between floats is in a specified range than to be exact equality. – Ichabod Clay Nov 20 '12 at 13:48
  • @JeppeStigNielsen sorry, i was meaning "compare equality" as Ichabod Clay said =) – fnurglewitz Nov 20 '12 at 13:54
  • @IchabodClay Maybe it's better to do so, but it doesn't entirely eliminate the possibility of precision "mysteries". For example if you check if `Math.Abs(x - 10.0) < 0.1` (because someone considers it bad to say just `x == 10.0`), you still get the "paradox" that some `x` with the string representation `"9.9"` pass the test while other `x` with identical string representation, do not. So is that a reason to not use floating-point numbers at all? I think not. – Jeppe Stig Nielsen Nov 20 '12 at 13:59
  • @tr3 But doesn't "compare less than or greater than" have the exact same issues? See my comment to IchabodClay. – Jeppe Stig Nielsen Nov 20 '12 at 14:01
  • @JeppeStigNielsen I suppose in this particular example with such a short float number that it would be better to use a `Decimal` so as to be less confusing. In any case, the code provided in the question asked about floats. The best we can do is give κωστας Σοφός some suggestions as to what might fit his design best :) – Ichabod Clay Nov 20 '12 at 14:08
  • @IchabodClay You know I just chose a short example to not have to type too much in this comment box. But the principle is the same for any floating-point number. I agree `Decimal` is better for many purposes, but it has other issues. For example `(1m / 3m) * 3m` gives an error that doesn't occur with `Double`. As for Kostas Sofos' original question, his problem was that he was comparing `858500000000000.0` to `8585.0`, so it wasn't really related to precision loss in number representations. (Therefore I've been offtopic all the time.) – Jeppe Stig Nielsen Nov 20 '12 at 14:31

4 Answers4

12

This is most probably related with your locale. Try this, it should work

double num1 = Convert.ToDouble(str1,CultureInfo.InvariantCulture);
double num2 = Convert.ToDouble(str2,CultureInfo.InvariantCulture);

Alo try printing your numbers, you will see the difference.

L.B
  • 114,136
  • 19
  • 178
  • 224
5

The reason is that you are running it on a machine that uses the comma as decimal character, not the dot. When you change your code to the following, it will print Equal.

string str1 = "85,8500000000000";
string str2 = "85,85";

This again shows why you always should specify a culture in methods like this. Your original code will work with dots when you specify CultureInfo.InvariantCulture:

string str1 = "85.8500000000000";
string str2 = "85.85";
double num1 = Convert.ToDouble(str1, CultureInfo.InvariantCulture);
double num2 = Convert.ToDouble(str2, CultureInfo.InvariantCulture);

CultureInfo is in the namespace System.Globalization.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Thanks a lot my friend. I have dot because i read these values from csv files. Thank you again for your advise – amigo Nov 20 '12 at 13:30
1

I think it is because of your current locale. Did you ever look into these values?

For me in Germany, the first number is 858500000000000, the second is 8585.

fero
  • 6,050
  • 1
  • 33
  • 56
0

A guess would be that the CurrentCulture of your thread has a NumberFormatInfo where the NumberDecimalSeparator is not ".".

If you use Convert.ToDouble(str1, System.Globaliztion.CultureInfo.InvariantCulture) the local culture of your thread will be disregarded.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181