简体   繁体   English

将十进制数据类型值转换为格式化的字符串

[英]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. 我想将Label22.TextLabel21.Text的结果转换为两位小数的数字。 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();
}

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());

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM