Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0292 Numeric Format Strings G or g

G or g General using System; using System.Text; using System.Globalization; class Sample { public static void Main() { Console.WriteLine("{0:G}", 1.2345); Console.WriteLine("{0:G}", 0.00001); Console.WriteLine("{0:g}", 1.2345); Console.WriteLine("{0:G3}", 1.2345); Console.WriteLine("{0:G}", 12345); } } The output: 1.2345 1E-05 1.2345 1.23 12345 Format switches to exponential notation for small or large numbers. G3 limits precision to three digits in total (before + after point).