C#格式化成小数

来源:互联网 发布:知乎 白诗诗是谁 编辑:程序博客网 时间:2024/04/29 23:14

datagridview某列格式化成两位小数

.............................................................................................................................................................

datagridView.columns[i]/defaultCellStyle.Format='f";

----------------------------------------------------------------------------------------------------------------------

Math.Round方法

应该还有math.ceiling和floor
double d =12345678.90123;
2d.ToString("F2");    //d=12345678.90
3string s=d.ToString("###,###.00");   //s=12,345,678.90

Math.Round(4.4);   //Returns   4.0.  
  Math.Round(4.5);   //Returns   4.0.  
  Math.Round(4.6);   //Returns   5.0.  
Math.Round(45.367,2)//Returns   45.37  
double   d=1.12345;  
  d=double.Parse(d.ToString("0.00"));  
  这样后,d=1.12

 

var   num   =   3.14159;  
  num   =   Math.round(num   *   100)   /   100;

C#下如果显示保留小数位数,及百分号的解决方法:

1、用NumberFormatInfo类来解决.
        System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();

        provider.PercentDecimalDigits = 2;//小数点保留几位数.
        provider.PercentPositivePattern = 2;//百分号出现在何处.
        double result = (double)1 / 3;//一定要用double类型.
        Response.Write(result.ToString("P", provider));

2、用toString方法.
public string getRate(double hcount, double task)
    {
        string rValue;
        string temp="";
        if (task == 0)
            task = 1;
        double db = (hcount / task) * 100;
        if (hcount >= task)
            rValue = "100%";
        else
            rValue = db.ToString("#0.#0") + "%"; ;
        return rValue;
    }

string str1 = String.Format("{0:N1}",56789); //result: 56,789.0
string str2 = String.Format("{0:N2}",56789); //result: 56,789.00
string str3 = String.Format("{0:N3}",56789); //result: 56,789.000
string str8 = String.Format("{0:F1}",56789); //result: 56789.0
string str9 = String.Format("{0:F2}",56789); //result: 56789.00
string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89
string str12 =(56789 / 100).ToString("#.##"); //result: 567

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wahaccp/archive/2008/12/01/3419277.aspx

原创粉丝点击