Problems with ... Convert ?

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problems with ... Convert ?

  1. #1
    Join Date
    Dec 2003

    Problems with ... Convert ?

    Hi guys
    I am getting a very strange error.
    I have a C# application that has this code :

    double a = 0;
    double b = 0;
    double c = 0;

    In the code, the values for "a", "b" and "c" changes and gets these values:

    a = 1716
    b = Convert.ToDouble(and gets a value from the database, in this case the value in the field is 0) so b will look like this :
    b = Convert.ToDouble(0)

    and c is like b but the value in the database field is 1716 so:

    b = Convert.ToDouble(1716)

    Now we have this situation :

    a = 1716
    b = 0
    c = 1716

    Later in the code there's a formula like this :

    a -= (b + c)

    Theorically I would get a value of 0 (zero) for "a" because :

    a = 1716 - (0 + 1716)


    a = 0

    The problem is that I'm not getting 0 but a very strange value like this :


    that is equal to : 0,00000000556453 (or somethink like that)

    And in the code if value of "a" is different from 0 it does not allow you to continue with other options, but I need to continue with those options because for me, "a" is equal to 0.
    The questions are :

    how is it possible that 1716 - (0 + 1716) is not equal to 0 but equal to -2,273456E-13 ?!?!?!?!?!?!?
    What's going on ?, what's wrong ?, how can I solve this problem ? is it a problem related to the Convert.ToDouble ?
    At this point I can't check if the value of "a" is <= 0 to set the value for "a" to 0 because "a" can contain negative values, so I don't know how to solve this problem.
    Any Ideas ?

    Thank you all

  2. #2
    Join Date
    May 2005
    The binary representation is not the same as an integer, when the convert to double happens and then is used in the calculation it gives a double which is reasonable, but in your calculation as an example you expectthe result to be a integer on your check you will have to compare by converting it back to an integer.. Or compare the value with a double
    I have come across a small marginal difference when converting integers to double

Similar Threads

  1. convert String to Calendar
    By Force Flow in forum Java
    Replies: 4
    Last Post: 06-29-2006, 02:01 PM
  2. Replies: 0
    Last Post: 07-19-2002, 02:41 AM
  3. convert email messages to tasks utomatically
    By Anthony Meo in forum Enterprise
    Replies: 0
    Last Post: 01-15-2002, 05:44 PM
  4. SQL syntax error
    By Ted Young in forum Database
    Replies: 7
    Last Post: 08-07-2001, 04:53 PM
  5. Convert to Hex
    By Sarel j Smit in forum Database
    Replies: 1
    Last Post: 05-24-2001, 11:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center