ToString()格式和用法大全,C#实现保留两位小数的方法

C,货币,2.5.ToString("C"),¥2.50。
D,十进制数,25.ToString("D5"),00025。
E,科学型,25000.ToString("E"),2.500000E+005。
F,固定点,25.ToString("F2"),25.00。
G,常规,2.5.ToString("G"),2.5。
N,数字,2500000.ToString("N"),2,500,000.00。
X,十六进制,255.ToString("X")。
FF,formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看),必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。

常用格式举例:

static void Main(string[] args)
        {
            int i1 = 12345;
            Console.WriteLine(i1.ToString());//结果 12345(this指当前对象,或叫当前类的实例)
            Console.WriteLine(i1.ToString("d8"));//结果 00012345

            int i2 = 123;
            double j = 123.45;
            string s1 = string.Format("the value is {0,7:d}", i2);
            string s2 = string.Format("the value is {0,7:f3}", j);
            Console.WriteLine(s1);//结果 the value is 123
            Console.WriteLine(s2);//结果 the value is 123.450

            double i3 = 12345.6789;
            Console.WriteLine(i3.ToString("f2")); //结果 12345.68
            Console.WriteLine(i3.ToString("f6"));//结果 12345.678900

            double i4 = 12345.6789;
            Console.WriteLine(i4.ToString("n")); //结果 12,345.68
            Console.WriteLine(i4.ToString("n4")); //结果 12,345.6789

            double i5 = 0.126;
            string s = string.Format("the value is {0:p}", i5);
            Console.WriteLine(i5.ToString("p")); //结果 12.6%
            Console.WriteLine(s); //结果 the value is 12.6%

            DateTime dt = new DateTime(2003, 5, 25);
            Console.WriteLine(dt.ToString("yy.M.d"));//结果 03.5.25
            Console.WriteLine(dt.ToString("yyyy年M月"));//结果 2003年5月

            int i6 = 123;
            double j6 = 123.45;
            string s6 = string.Format("i:{0,-7},j:{1,7}", i6, j6);//-7表示左对齐,占7位
            Console.WriteLine(s6);//结果i:123 ,j: 123.45
        }

 
DataBinder.Eval用法范例
//显示二位小数
//<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>
//{0:G}代表显示True或False
<%# DataBinder.Eval(Container.DataItem, "Discontinued", "{0:G}") %>
//转换类型
((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4)
{0:d} 日期只显示年月日
{0:yyyy-mm-dd} 按格式显示年月日
{0:c} 货币样式
<%#DataBinder.Eval(Container.DataItem,"h_yk", "${0:F2}") %>美元

如何设定全局变量
Global.asax中Application_Start()事件中添加Application[属性名] = xxx;就是你的全局变量

添加一个编号列:

            DataTable dt = c.ExecuteRtnTableForAccess(sqltxt); //执行sql返回的
            DataTable DataColumn dc = dt.Columns.Add("number",System.Type.GetType("System.String"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["number"] = (i + 1).ToString();
            }
            DataGrid1.DataSource = dt;
            DataGrid1.DataBind();

 
DataGrid1中添加一个CheckBox,页面中添加一个全选框

        private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
        {
            foreach (DataGridItem thisitem in DataGrid1.Items)
            {
                ((CheckBox)thisitem.Cells[0].Controls[1]).Checked = CheckBox2.Checked;
            }
        }


获取错误信息并到指定页面
不要使用Response.Redirect,而应该使用Server.Transfer

        // 在 global.asax 中
        protected void Application_Error(Object sender, EventArgs e)
        {
            if (Server.GetLastError() is HttpUnhandledException)
                Server.Transfer("MyErrorPage.aspx");
            //其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
        }

Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理


C# 实现保留两位小数的方法
1、Math.Round(0.333333,2);//按照四舍五入的国际标准
2、double dbdata=0.335333; string str1=String.Format("{0:F}",dbdata);//默认为保留两位
3、float i=0.333333; int j=(int)(i * 100); i = j/100;
4、decimal.Round(decimal.Parse("0.3333333"),2)
5、private System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); float test=0.333333f; nfi.NumberDecimalDigits=2; string result=test.ToString("N", nfi);
6、string result= String.Format("{0:N2}",Convert.ToDecimal("0.333333").ToString());

猜你喜欢

转载自www.cnblogs.com/cnwuchao/p/10586725.html
今日推荐