zoukankan      html  css  js  c++  java
  • ToString() 格式 用法大全 保留 两位 小数

    ToString() 格式 用法大全 保留 两位 小数

    ToString()格式和用法大全

    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 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)

    必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。

    常用格式举例:

    (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[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());
  • 相关阅读:
    MySQL的删除语句
    机器学习-K-means聚类及算法实现(基于R语言)
    机器学习-线性回归补充-实践
    什么是JWT
    Java的NIO
    音乐播放器歌词同步显示
    Linux和Mac下安装RocketMQ过程
    支付宝沙箱应用
    HTML5 调用手机摄像头拍照
    JavaWeb图片上传的几种方式
  • 原文地址:https://www.cnblogs.com/zhangji/p/2756938.html
Copyright © 2011-2022 走看看