// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; class MainClass { private static void test(double d, int ndig) { string[] fmt = { "#", "#.0", "#.00", "#.000", "#.000", "#.0000", "#.00000", "#.0000000" }; Console.WriteLine(d.ToString(fmt[ndig])); } public static void Main(string[] args) { test(123.456789, 3); test(123.0000012, 3); test(123, 3); test(123.456789, 2); test(123.0000012, 2); test(123, 2); } }