moe
moe

Reputation: 5249

How to get decimal results

I am expecting to get as decimal point like 30.769 but i am only getting 30 in this case. I have tried to change my int myVar to double myVar but got an error. I have done some research but could not get it right so far. I know i have make changes in here:

int myVar= (int)ttl_Res.ExecuteScalar();

and also here:

COUNT(DDL_ANS)*100/@myVar

But here is my current code:

   if (TYPE.SelectedValue == "sometype")
            {
                con.Open();
                int myVar= (int)ttl_Res.ExecuteScalar();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Update SETS  set SCORE = x.Result from (select ID, Type, COUNT(DDL_ANS)*100/@myVar AS Result from JATG) as x where ID = 1211";
                cmd.Parameters.Add("@myVar", SqlDbType.VarChar).Value = myVar;
                cmd.Connection = con;
                //con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
    } 

Upvotes: 1

Views: 233

Answers (3)

TomBot
TomBot

Reputation: 1146

The reason why COUNT(DDL_ANS)*100/@myVar returns an int is because all three operands (COUNT(DDL_ANS), 100 and @myVar) are int. To get a decimal result it should be sufficient to cast one operand to decimal, for example COUNT(DDL_ANS)*CONVERT(DECIMAL,100)/@myVar.

Upvotes: 2

Transcendent
Transcendent

Reputation: 5755

You have to use the Decimal instead.

var myVar= (Decimal) ttl_Res.ExecuteScalar(); // Assuming your first column in the first row your query contains a value with decimal points

Upvotes: 0

dotarmin
dotarmin

Reputation: 1

You can explicitly convert it to a double with Convert.ToDouble. The error could be because of an overflow but it depends what the integer is.

Upvotes: 0

Related Questions