JohnChris JohnChris - 1 year ago 101
C# Question

Math.Sqrt(2/3) returns 0

I am really confused why this is happening, my code:

double x = Math.Sqrt(2/3);

Displays 0.
The answer is

0.8164, I know I will also have to use Math.Round to round this up, but for the moment the issue is I'm getting 0

Answer Source

The problem is caused by automatic integer evaluation of the numbers. Use:

double x = Math.Sqrt(2f/3f);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download