How to get floats value without including exponential notation

后端 未结 5 1070
自闭症患者
自闭症患者 2021-01-02 01:04

In C#, is it possible to perform ToString on a float and get the value without using exponentials?

For example, consider the following:

float dummy;
         


        
5条回答
  •  有刺的猬
    2021-01-02 01:28

    float dummy = 0.000006F;
    Console.WriteLine(dummy.ToString("0." + new string('#', 60)));
    

    If you'll be doing this a lot then it makes sense to store the format string in a static field/property somewhere and re-use it, rather than constructing a new string every time:

    private static readonly string _allFloatDigits = "0." + new string('#', 60);
    
    // ...
    
    float dummy = 0.000006F;
    Console.WriteLine(dummy.ToString(_allFloatDigits));
    

提交回复
热议问题