zoukankan      html  css  js  c++  java
  • ToString()格式和用法大全

    C

    ToString()格式和用法大全 
    货币 
    2.5.ToString("C") 
    ¥2.50 

    十进制数 
    25.ToString("D5") 
    00025 

    科学型 
    25000.ToString("E") 
    2.500000E+005 

    固定点 
    25.ToString("F2") 
    25.00 

    常规 
    2.5.ToString("G") 
    2.5 

    数字 
    2500000.ToString("N") 
    2,500,000.00 

    十六进制 
    255.ToString("X") 
    FF 
    formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看) 必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。 
    常用格式举例: 
    (1) int i=12345; 
    this.textBox1.Text=i.ToString(); 
    //结果 12345(this指当前对象,或叫当前类的实例) 
    this.textBox2.Text=i.ToString("d8"); 
    //结果 00012345 
    (2) int i=123; 
    double j=123.45; 
    string s1=string.Format("the value is {0,7:d}",i); 
    string s2=string.Format("the value is {0,7:f3}",j); 
    this.textBox1.Text=s1 ; 
    //结果 the value is 123 
    this.textBox2.Text=s2; 
    //结果 the value is 123.450 
    (3)double i=12345.6789; 
    this.textBox1.Text=i.ToString("f2"); //结果 12345.68 
    this.textBox2.Text=i.ToString("f6"); 
    //结果 12345.678900 
    (4)double i=12345.6789; 
    this.textBox1.Text=i.ToString("n"); //结果 12,345.68 
    this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789 
    (5)double i=0.126; 
    string s=string.Format("the value is {0:p}",i); 
    this.textBox1.Text=i.ToString("p"); //结果 12.6% 
    this.textBox2.Text=s; //结果 the value is 12.6% 
    (6) DateTime dt =new DateTime(2003,5,25); 
    this.textBox1.Text=dt.ToString("yy.M.d"); 
    //结果 03.5.25 
    this.textBox2.Text=dt.ToString(“yyyy年M月”); 
    //结果 2003年5月 
    (7) int i=123; 
    double j=123.45; 
    string s=string.Format("i:{0,-7},j:{1,7}",i,j); 
    //-7表示左对齐,占7位 
    this.textBox1.Text=s ; 
    //结果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 ["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的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理
  • 相关阅读:
    选择排序
    转:ASP.NET MVC中Unobtrusive Ajax的妙用
    转:MVC 下导航超链接本页面高亮的一种解决方案
    转载:iOS 推送的服务端实现
    转载:Unobtrusive JavaScript in ASP.NET MVC 3 隐式的脚本在MVC3
    待实践三:MVC3下 路由的测试 使用 RouteDebug.dll 来测试判断路由是否符合
    待实践二:MVC3下的3种验证 (1)前台 jquery validate验证 (2)MVC实体验证 (3)EF生成的/自己手写的 自定义实体校验(伙伴类+元素据共享)
    待实践一:仿造博客园后台上传头像并切图生成缩略图fine uploader.js jcrop.js
    Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据
    Linq 时间对比陷阱坑
  • 原文地址:https://www.cnblogs.com/ruiati/p/2863917.html
Copyright © 2011-2022 走看看