zoukankan      html  css  js  c++  java
  • C#格式化成小数

    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

  • 相关阅读:
    使用 Eclipse 调试 Java 程序的 10 个技巧
    oracle9i,10g再谈优化模式参数问题.
    oracle 索引
    解决IE不能在新窗口中向父窗口的下拉框添加项的问题
    获取文档的尺寸:利用Math.max的另一种方式
    揭开constructor属性的神秘面纱
    测试杂感:Windows8也许需要Account Hub
    探索式测试:探索是为了学习
    一次有教益的程序崩溃调试 (下)
    软件测试读书列表 (2013.8)
  • 原文地址:https://www.cnblogs.com/dachie/p/1739958.html
Copyright © 2011-2022 走看看