-14

Is there a way in C# for returning a square root of a number with decimals. Every time I use Math.Sqrt() it doesn't return a decimal.

My code:

Int32 a = Convert.ToInt32(input1.Text);
        Int32 b = Convert.ToInt32(input2.Text);

        //a*a + b*b = c*c
        Int32 a2 = a * a;
        Int32 b2 = b * b;
        Int32 c2 = a2 + b2;
        Int32 c = Convert.ToInt32(Math.Sqrt(c2));
        output.Text = Convert.ToString(c);
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 3
    The [`Math.Sqrt`](https://msdn.microsoft.com/en-us/library/system.math.sqrt%28v=vs.110%29.aspx) methods returns a double, so that definitely contains the fractional part of the result. How are you using it? – Guffa Sep 13 '15 at 12:00
  • decimal is not desktop hardware's capability. you need to convert it from float or double to decimal. but some special cpu architecture may have ability of fixed point arithmetics in which case you need to implement your own sqrt. – huseyin tugrul buyukisik Sep 13 '15 at 12:01
  • This question has several answers here: http://stackoverflow.com/questions/4124189/performing-math-operations-on-decimal-datatype-in-c – user102 Sep 13 '15 at 12:02
  • The duplicate is not appropriate, since the question has now clearly nothing to do with the `decimal` type. Reopening so a better decision can be made. – Frédéric Hamidi Sep 13 '15 at 12:04
  • you are converting to int32 which truncates the decimal places! – Daniel A. White Sep 13 '15 at 12:06
  • 1
    You're clearly converting the result of the `Math.Sqrt()` to an `Int32` - an integer, i.e. no decimals. – Iridium Sep 13 '15 at 12:07
  • @trickrider2002: You should use `int` instead of `Int32` and I can't see what for you need the decimal when dealing with `int`s? Also converting the `c` value to `int` will loose precision in most cases. I suggest to used `double` for `c`. – Matyas Sep 13 '15 at 12:08
  • 1
    Because your question deserves them. You have Int32 c = Convert.ToInt32(Math.Sqrt(c2)); and asking what happened to my decimal fraction needs down votes. – Adarsha Sep 13 '15 at 12:21

2 Answers2

2

This line in your code:

  Int32 c = Convert.ToInt32(Math.Sqrt(c2));

does two type conversions. One of them is implicit: c2, an Int32 item, gets converted to Double so it can be operated upon by Math.Sqrt(). So far so good.

But then you convert the result, which almost certainly has a fractional part represented in the Double output, to an Int32. This scrubs away the fractional part.

C# has some really useful formatting capabilities. Read the examples on this page. https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx It's possible that using

output.Text = Math.Sqrt(c2).ToString("F4"); 

will give you a helpful result.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
1

Casting to integer (Int32) drops the fractional part of your number...

    int number = (Int32) 3.14159;  // value of number will be 3 (not 3.14159)
    // Do this instead
    double c = Math.Sqrt(c2); // will give you the result you want
    output.Text = Convert.ToString(c);
Samuel
  • 61
  • 5