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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM