override .ToString()

c#, overriding

Solution

Why not just use the built-in formatting?

var pi = 3.14159265m;
Console.WriteLine(pi.ToString("N5"));
-> "3.14159"

For reference I like the .NET Format String Quick Reference by John Sheehan.

Problem

I would like to override the .ToString() function so that whenever I get a double it outputs only 5 digits after the decimal point. How do I reffer to the object the .ToString is working on, inside the override function? In other words what shell I put instead of the XXX in the following code? ``` public override string ToString() { if (XXX.GetType().Name == "Double") return (Math.Round(XXX, 5)); } ```

Original source

Related problems