What format string displays float as is?

I have a float of about 27 significant digits, when I call "ToString ()" I get "6.8248054E + 26", how do I get the exact value?

+8
c #
source share
5 answers
var number = 0.111111111100000000000000000; string result = String.Format("{0:#,0.###########################}", number); 

This will show all decimal numbers up to the 27th, but omits all trailing zeros. Thus, the indicated number will be displayed as 0.1111111111.

+6
source share

I know that you want to display the 27 digit number and which you use float . You can always do this:

 f.ToString("F27"); 

But really, they just don't match . To achieve this accuracy, use decimal :

 decimal dc = 91.123142141230131231231M; //your 27-digit figure: dc.ToString("F27"); 
+8
source share

try it.

 number.ToString("F27"); 
+2
source share

The problem is not what format you use. It is rather related to the accuracy of the data type.

From MSDN:

float has an accuracy of 7 digits. decimal on the other hand has an accuracy of 28-29 digits.

If you specify your value as a decimal variable, even if you call the ToString() method without any string formats, you will get what you want.

 dc = 6.8248054E+26M; Console.WriteLine(dc.ToString()); // returns the whole thing including any trailing zero(s) 
+2
source share

With floats you cannot get this accuracy.

In any case, you must use the formatter "F" or "N", they will print the number as you wish.

Here you can check all forms: Standard number format strings

-2
source share

All Articles