Ask Your Question

How should a System.Double be compared to the number 0 or an integer?

asked 2022-07-01 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-02-04 01:00:00 +0000

lakamha gravatar image

A System.Double should be compared to the number 0 or an integer by using the Double.Equals() method or by subtracting the number from the Double and comparing the absolute value of the difference to a small tolerance value to account for rounding errors. For example:

// using Double.Equals() double num1 = 0.0; double num2 = 1.234567; if (Double.Equals(num1, num2)) { Console.WriteLine("The numbers are equal"); }

// using tolerance value double num3 = 3.0; int num4 = 3; double tol = 0.0001; if (Math.Abs(num3 - num4) < tol) { Console.WriteLine("The numbers are equal"); }

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-07-01 11:00:00 +0000

Seen: 18 times

Last updated: Feb 04 '22