Override .ToString ()

I would like to override the .ToString () function so that whenever I get double it only outputs 5 digits after the decimal point.

How can I refer to the object that .ToString is working on inside the override function? In other words, which shell do I put instead of XXX in the following code?

public override string ToString() { if (XXX.GetType().Name == "Double") return (Math.Round(XXX, 5)); } 
+4
override c #
source share
7 answers

Why not just use inline formatting?

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

For reference, I like it . Quick Reference for NET Format Strings by John Sheehan.

+20
source share

You cannot override a method for another type - basically you cannot stop double.ToString by doing what it does. You can, however, write a method that takes a double and formats it appropriately.

+11
source share

As John pointed out, you cannot override Double.ToString . You can create an extension method for the double type to help you do this:

 public static class DoubleExtensions { public static string ToStringWith5Digits(this double x) { ... } } 

and use it like:

 double d = 10; string x = d.ToStringWith5Digits(); 
+6
source share

You can pass a Double.ToString() format argument by specifying the number of digits:

 double x = 5.12345667; string s = x.ToString("F5"); // s="5.12345", x.ToString("#.#####") also works 

You cannot override the ToString () function for doubles (this is a struct Double member function)

+2
source share

What class is your ToString () function in? Usually you have a private variable inside the class you are using. (But it can also be a combination of variables to build ToString ())

for example: (pseudo code is this)

 class MyClass { private double dd; private string prefix = "MyDouble:"; ... ... public override string ToString() { if (dd.GetType().Name == "Double") return ( prefix + Math.Round(dd, 5).ToString() ); } } 
0
source share

you cannot override, but you can use the extension method

 public static string ToRoundString(this double doubleValue) { return Math.Round(doubleValue, 5).ToString(); } 

Using

 public void TestMethod() { double greatValue = 3; string newString = greatVale.ToRoundString(); } 

amuses

0
source share

Although "F5" or "#. #####" decides the specifics of the original post, as the title is wider ("override.ToString ()"), I thought I'd add that you can also create an extension method that overloads ToString() .

So, for example, the extension method:

  public static string ToString(this double value, int roundTo, string roundSuffix) { string rounded = value.ToString($"#.{new String('#', roundTo)}"); if (rounded != value.ToString()) { rounded = $"{rounded}{roundSuffix}"; } return rounded; } 

Will produce "5.25 was rounded" if passed

 double d = 5.2514; string formatted = d.ToString(2, " was rounded"); 

or "5.2" if passed

 double d = 5.2; string formatted = d.ToString(2, " was rounded"); 

(just by chance there is some strange case when someone wants to do something like that!)

However, you should not try to override the ToString() method with a method that has the same signature as one of the built-in ToString() overloads, because while the IDE sees it, it will never call the extension method (see How to call the extension method, which has the same name as the existing method? for details)

0
source share

All Articles