PROWAREtech

articles » current » dot-net » double-to-string-without-scientific-notation

.NET: Double to String Conversion Without Scientific Notation

How to print double values without using scientific notation, in C#.

Converting the double to a decimal work best. Run this code and see how well it works:


static void Main(string[] args)
{
	double print;

	print = 1234.0000000505320;
	Console.WriteLine(print.ToString("F99").TrimEnd('0')); // prints 1234.00000005053198037785477936267852783203125
	Console.WriteLine((decimal)print);                     // prints 1234.00000005053

	print = 43E-20;
	Console.WriteLine(print.ToString("F99").TrimEnd('0')); // prints 0.000000000000000000430000000000000023059522565938484126443152389177287248100967076425149571150541306
	Console.WriteLine((decimal)print);                     // prints 0.00000000000000000043

	print = 23E20;
	Console.WriteLine(print.ToString("F99").TrimEnd('0'));
	Console.WriteLine((decimal)print);

	print = -23E-20;
	Console.WriteLine(print.ToString("F99").TrimEnd('0'));
	Console.WriteLine((decimal)print);

	print = 13E-20;
	Console.WriteLine(print.ToString("F99").TrimEnd('0'));
	Console.WriteLine((decimal)print);
}

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE