C#格式化成小数

    技术2024-10-03  53

    datagridview某列格式化成两位小数

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

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

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

    Math.Round方法

    应该还有math.ceiling和floor double d =12345678.90123;2d.ToString("F2");    //d=12345678.903string 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.0string str2 = String.Format("{0:N2}",56789); //result: 56,789.00string str3 = String.Format("{0:N3}",56789); //result: 56,789.000string str8 = String.Format("{0:F1}",56789); //result: 56789.0string str9 = String.Format("{0:F2}",56789); //result: 56789.00string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89string str12 =(56789 / 100).ToString("#.##"); //result: 567

     

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

    最新回复(0)