Is it possible to define a default number format that is used whenever I convert an integer
(or double
etc.) to a String
without specifying a format string?
C# example:
int i = 123456;
string s = "Hello " + i;
// or alternatively with string.Format without format definition
string s = string.Format("Hello {0}", i);
ASP.NET Razor example:
<div>
Hello @i
</div>
I think all these code lines implicitly use the default ToString()
method for Int32
. And not surprisingly, all these code lines result in "Hello 123456"
, but I want "Hello 123,456"
.
So can I specify that "N0"
should be used by default (at least for integer
)?
I already found the question Set Default DateTime Format c# - it looked good, but it doesn't help me for numbers.
Edit: I'm aware that I could write an extension method which I can use throughout my application, but this is not what I'm looking for. I would like to find a property (maybe somewhere hidden in CultureInfo
or NumberFormatInfo
) which is currently set to "G"
and is used by the default Int32.ToString()
implementation.
@Html.DisplayFor
. For some WinForms controls you can specify an attribute on a class property. – Veatsint
types are implemented asstruct
in the background. They cannot be inherited from or overridden easily.CultureInfo
does not support this as the defaultToString()
method uses theG
format, which does not allow for thousand seperator. So, No, this is not possible without extension methods. I would love to be proven wrong though. – Frady