我想将Label22.Text
和Label21.Text
的结果转换为带有两位小数的数字。尝试了很多变种
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();
}
答案 0 :(得分:1)
试试这个
Label22.Text = string.Format("{0:N2}", Convert.ToDecimal(dr["D"].ToString()));
Label21.Text = string.Format("{0:N2}", Convert.ToDecimal(dr["C"].ToString()));
也试试这个
Label22.Text = string.Format("{0:0.00}", dr["D"].ToString());
Label21.Text = string.Format("{0:0.00}", dr["C"].ToString());
答案 1 :(得分:1)
您也可以在查询中执行此操作:
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