浮点数怎么取小数点前两位?

解决方案 »

  1.   

    float bf1=1234.5678;
    string bs1=bf1.ToString("0.00");
      

  2.   

    public static string CommaSeparatedValues(string sourceText, int numberPoint, int decimalPoint)
    {
    string retValue = "";
    double d; if ( decimalPoint < 0 )
    {
    retValue = "0";
    }
    else
    {
    //Check Number
    if ( Double.TryParse(sourceText, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out d) )
    {
    retValue = String.Format("{0:n" + decimalPoint.ToString() + "}", d);

    //Check Value About decimalPoint
    string checkValue = retValue.Replace(",", ""); if ( decimalPoint > 0 )
    {
    if ( checkValue.IndexOf('.') > numberPoint )
    {
    retValue = String.Format("{0:n" + decimalPoint.ToString() + "}", 0);
    }
    }
    else
    {
    if ( checkValue.Length > numberPoint )
    {
    retValue = String.Format("{0:n" + decimalPoint.ToString() + "}", 0);
    }
    }
    }
    else
    {
    retValue = String.Format("{0:n" + decimalPoint.ToString() + "}", 0);
    }
    } //Result
    return retValue;
    }
      

  3.   


    (System.Math.Abs(7.8908)-System.Math.Abs(System.Math.Floor(7.8908))).ToString().Substring(0,4)
    这样对于7.8908就可以得到0.89了.
    呵呵,可能比较麻烦:(
      

  4.   

    简单点的float bf1=1234.5678f;
    float bf2=bf1*100;
    int bi1=(int)bf2;
    bf1=(float)bi1/(float)100;

    this.Text=bf1.ToString();
      

  5.   

    float [ ( n ) ]从 - 1.79E + 308 到 1.79E + 308 之间的浮点数字数据。n 为用于存储科学记数法 float 数尾数的位数,同时指示其精度和存储大小。n 必须为从 1 到 53 之间的值。