Reputation: 1518
I have two values coming from database i need to convert these values into double i have done like this i ahve just mentioned like this just as local variables
string val;
string dpayment;
and i am taking the values from database by the following query
if (dt != null)
{
if (dt.Rows.Count > 0)
{
dpayment = Convert.ToDouble(dt.Rows[1]["monthlyamount"]).ToString("0.00");
val= Convert.ToInt32( dt.Rows[3]["value"]).ToString();
}
}
i am using these values and doing the operation like this
double eqvperiod = Convert.ToDouble(val / dpayment).ToString();
but it was giving error
operator '/' cannot be used as a operands of type string and string.
any one can help on this
my final result is i want to convert the values coming from databases to double and i have to do the operations on this values
Upvotes: 0
Views: 1214
Reputation: 559
How about this?
double eqvperiod = 0.00;
if (dt != null && dt.Rows.Count > 0)
{
eqvperiod = Convert.ToDouble(dt.Rows[1]["monthlyamount"]) /
Convert.ToInt32(dt.Rows[3]["value"]);
}
Upvotes: 0
Reputation: 1
Try this:
double eqvperiod = (Convert.ToDouble(val) / Convert.ToDouble(dpayment));
Upvotes: 0
Reputation: 71
You can also do double.Parse("0.010")
this also works for int, short, decimal, and probably any other numeric types
Upvotes: 0
Reputation: 77530
Convert to double
before dividing:
double eqvperiod = Convert.ToDouble(val) / Convert.ToDouble(dpayment);
And actually, you don't need to Convert()
then ToString()
then Convert()
a second time:
int val = 0;
double dpayment = 0.0;
if (dt != null && dt.Rows.Count > 0)
{
dpayment = Convert.ToDouble(dt.Rows[1]["monthlyamount"]);
val= Convert.ToInt32( dt.Rows[3]["value"]);
}
double eqvperiod = val / dpayment;
Upvotes: 2
Reputation: 6793
Why have you declared val and dpayment as strings? If you want them to be numeric types then change the variable declaration and remove the ToString, i.e.
int val;
double dpayment;
dpayment = Convert.ToDouble(dt.Rows[1]["monthlyamount"]);
val= Convert.ToInt32( dt.Rows[3]["value"]);
Upvotes: 2