callasda
callasda

Reputation: 89

Converting a decimal data type value to a formatted string

I want to convert the results of Label22.Text and Label21.Text to a number with two decimal places. Have tried a number of variations

sda = new SqlDataAdapter(@"SELECT fname, SUM([debit]/1.2) AS [D], SUM([credit]/1.2) AS [C] FROM detail GROUP BY fname", con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataRow dr = ds.Tables[0].Select("fname = '0092632'").FirstOrDefault();

if (dr != null)
{
    Label22.Text = dr["D"].ToString();
    Label21.Text = dr["C"].ToString();
}

Upvotes: 1

Views: 551

Answers (2)

shA.t
shA.t

Reputation: 16958

You can also do it in your query:

SELECT 
    fname, 
    CONVERT(DECIMAL(20,2), SUM([debit]/1.2)) AS [D], 
    CONVERT(DECIMAL(20,2), SUM([credit]/1.2)) AS [C] 
FROM 
    detail 
GROUP BY 
    fname

Upvotes: 1

ThePravinDeshmukh
ThePravinDeshmukh

Reputation: 1913

Try this

Label22.Text = string.Format("{0:N2}", Convert.ToDecimal(dr["D"].ToString()));
Label21.Text = string.Format("{0:N2}", Convert.ToDecimal(dr["C"].ToString()));

Also try this

Label22.Text = string.Format("{0:0.00}", dr["D"].ToString());
Label21.Text = string.Format("{0:0.00}", dr["C"].ToString());

Upvotes: 1

Related Questions