Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I compare doubles?

How can I compare doubles?

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xkx32
    wrote on last edited by
    #1

    hello can you tell me how can I compare two double vars in my app i tried: if (var.Equals(.4)) but it's not working... and i am sure the two var contains the same value (i used MessageBox to print the vals) please help me

    N A S S 4 Replies Last reply
    0
    • X xkx32

      hello can you tell me how can I compare two double vars in my app i tried: if (var.Equals(.4)) but it's not working... and i am sure the two var contains the same value (i used MessageBox to print the vals) please help me

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello the Equals() method is usually used to compare two reference types -Yet it still can compare value types-. Instead you should take a shorter approach in comparing two value types by using the == operator

      double A = 5.4;
      double B = 2.3;

      if (A == B)
      {
      //Do something
      }
      else if(A == 2.7)
      {
      //Do somthing
      }

      Regards:rose:

      1 Reply Last reply
      0
      • X xkx32

        hello can you tell me how can I compare two double vars in my app i tried: if (var.Equals(.4)) but it's not working... and i am sure the two var contains the same value (i used MessageBox to print the vals) please help me

        A Offline
        A Offline
        Andrei Ungureanu
        wrote on last edited by
        #3

        hy, have you tried to compare something like: double a; double b; if (a <= b) { } if (a >= b) { } it works just fine with double as with integers. Do your best to be the best

        1 Reply Last reply
        0
        • X xkx32

          hello can you tell me how can I compare two double vars in my app i tried: if (var.Equals(.4)) but it's not working... and i am sure the two var contains the same value (i used MessageBox to print the vals) please help me

          S Offline
          S Offline
          Shajeel
          wrote on last edited by
          #4

          may be a better way to compare double will be if(Math.Abs(var-0.4) < 1e-14) Regards Shajeel

          1 Reply Last reply
          0
          • X xkx32

            hello can you tell me how can I compare two double vars in my app i tried: if (var.Equals(.4)) but it's not working... and i am sure the two var contains the same value (i used MessageBox to print the vals) please help me

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #5

            You better use decimal instead of double in such circumstances.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups