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!

10
  • 3
    They show as equal when I run this code Commented Nov 20, 2012 at 13:18
  • @κωστας Σοφός Are you sure, is the "." your system decimal separator? Commented Nov 20, 2012 at 13:22
  • 1
    also, try to avoid comparations between floating point numbers, since they tend to be a problem usually, see here: stackoverflow.com/questions/1398753/… Commented Nov 20, 2012 at 13:25
  • @tr3 What's the use of numbers if you're not allowed to compare them? Do you know of an application? Commented Nov 20, 2012 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. Commented Nov 20, 2012 at 13:48

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I should use a library i.e using **** to have access to CultureInfo.***
It is in System.Globalization
Thanks. It is working. I read this values from csv files and i want to compare these. Thanks again
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.

1 Comment

Thanks a lot my friend. I have dot because i read these values from csv files. Thank you again for your advise
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.