如何在DataTable中插入精确到两位小数的值 [英] How to Insert Values in DataTable with Exactly Two decimal places

查看:291
本文介绍了如何在DataTable中插入精确到两位小数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Page_Load( object  sender,EventArgs e)
{
try
{
DataTable dt = < span class =code-keyword> new DataTable();
dt.Columns.Add( SOT typeof运算(十进制));
dt.Columns.Add( SVT typeof运算(十进制));
dt.Columns.Add( SCT typeof运算(十进制));
dt.Rows.Add( 12 22 23 44 22 00 );
dt.Rows.Add( 2 20 23 00 22 23 );
dt.Rows.Add( 12 00 23 20 22 05 );
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (例外d)
{
Response.Write(d.Message.ToString());
}
}



DataTable可以使用{12,10.5}而不是{12.00,10.50}之类的值。是否有任何方法可以插入到DataTable中,只有两位小数。

解决方案

你知道, 12 12.00 是相同的十进制值。

你不应该为数字表示而烦恼数据库并在GUI上执行格式化作业。


 尝试 喜欢

dt.Rows.Add( 12 .22m, 23 .44m, 22 .00m);



Math.Round(( Double 12 22 2


protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("SOT", typeof(Decimal));
        dt.Columns.Add("SVT", typeof(Decimal));
        dt.Columns.Add("SCT", typeof(Decimal));
        dt.Rows.Add(12.22, 23.44, 22.00);
        dt.Rows.Add(2.20, 23.00, 22.23);
        dt.Rows.Add(12.00, 23.20, 22.05);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    catch (Exception d)
    {
        Response.Write(d.Message.ToString());
    }
}


DataTable can take values like {12,10.5} instead of {12.00,10.50}. Is there any method to insert into DataTable with exactly two decimal places.

解决方案

You know, 12 and 12.00 are the same Decimal value.
You shouldn't bother about number representation inside the database and do your formatting job on the GUI.


Try like below:

dt.Rows.Add(12.22m, 23.44m, 22.00m);

or

Math.Round((Double)12.22, 2)


这篇关于如何在DataTable中插入精确到两位小数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆