JohnChris - 3 days ago 5

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

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

```
double x = Math.Sqrt(2f/3f);
MessageBox.Show(x.ToString());
```

Source (Stackoverflow)

Comments