C# print a double/float value with a certain precision

Sometimes, we need to print double or float values with a certain precision, a.k.a. the number of decimal digits.

See below one way to do it in C#.

double number = 3.1415926536;

Console.WriteLine($"Printing with two decimal digits {number:N2}");

Console.WriteLine($"Printing with six decimal digits {number:N5}");

Console.WriteLine($"Printing with six decimal digits {number:N6}");

Console.WriteLine($"Printing with all decimal digits {number}");
print a float value with a certain precision output example
Print a float value with a certain precision output example

H@ppy Coding!