.NET Framework Developer Center > .NET Development Forums > .NET Base Class Library > float exponential value interms of 0.00 like ?????
Ask a questionAsk a question
 

Answerfloat exponential value interms of 0.00 like ?????

  • Friday, November 06, 2009 5:13 PMManasMSDN Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi guys i am getting value in exponential

    like :3.14765786987878E-6

    I need thsi value in exact way

    like : 0.00 some thing it has to come..,

    I searched in google and tried in all the ways but i didnt get solution...,

    plz help me,

Answers

  • Thursday, November 12, 2009 5:21 AMmpturn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try ToString("e") rather than ToString("F2"), if you would like to see your number output in exponential notation.  Further modification of the format string will allow you to limit the number of decimals shown, while still maintaining exponential notation.

    See for example http://msdn.microsoft.com/en-us/library/kfsatb94.aspx
  • Wednesday, November 11, 2009 8:43 AMeryangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,
    The output is correct, because
        3.14765786987878E-6 =  0.0000031476786987878
    then, statement:
        float.Parse("3.14765786987878E-6", NumberStyles.Float | NumberStyles.AllowExponent).ToString("F2")
    is equals to:
        float f = 0.0000031476786987878F;
        string s = f.ToString("F2");  // s = "0.00"

    that's why we got output 0.00.

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.

All Replies

  • Friday, November 06, 2009 5:28 PMJeffWask Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Are you saying you want to display the value as 0.00000314 or that you want everything rounded to 0.00?
  • Friday, November 06, 2009 5:41 PMManasMSDN Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

     

    Console.WriteLine("Float="+ float.Parse("3.14765786987878E-6", NumberStyles.Float | NumberStyles.AllowExponent).ToString("F2"));


    OUTPUT= 0.00;


    I think, when it is

    3.14765786987878E-6 =  0.0000031476786987878E

    but i want to display  in a very short  format like  0.31E
     
  • Wednesday, November 11, 2009 8:43 AMeryangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,
    The output is correct, because
        3.14765786987878E-6 =  0.0000031476786987878
    then, statement:
        float.Parse("3.14765786987878E-6", NumberStyles.Float | NumberStyles.AllowExponent).ToString("F2")
    is equals to:
        float f = 0.0000031476786987878F;
        string s = f.ToString("F2");  // s = "0.00"

    that's why we got output 0.00.

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
  • Thursday, November 12, 2009 5:21 AMmpturn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try ToString("e") rather than ToString("F2"), if you would like to see your number output in exponential notation.  Further modification of the format string will allow you to limit the number of decimals shown, while still maintaining exponential notation.

    See for example http://msdn.microsoft.com/en-us/library/kfsatb94.aspx