JohnChris - 1 year ago 58

C# Question

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

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

MessageBox.Show(x.ToString());

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);
MessageBox.Show(x.ToString());
```