8

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.

Community
  • 1
  • 1
fero
  • 6,050
  • 1
  • 33
  • 56
  • can you do something with cultureInfo.NumberFormat, in the way your example url uses DateTimeFormat ? – Tom Brown Feb 26 '14 at 12:45
  • 1
    This may be a bit too broad. Depending on the context, it's possible to do this. For MVC, you can create a DisplayTemplate and use `@Html.DisplayFor`. For some WinForms controls you can specify an attribute on a class property. – user247702 Feb 26 '14 at 12:45
  • @TomBrown No, this is why I posted my question. :-) – fero Feb 26 '14 at 12:46
  • Related: http://stackoverflow.com/questions/2109544/override-tostring – user247702 Feb 26 '14 at 12:48
  • Why don't you customize CultureInfo.CurrentCulture.NumberFormat? E.g. CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = "."; – Dmitry Bychenko Feb 26 '14 at 12:50
  • @DmitryBychenko That's not possible. *InvalidOperationException: Instance is read-only.* – user247702 Feb 26 '14 at 12:55
  • I was going to suggest an utterly filthy hack involving using Microsoft Fakes to shim Int32, but it turns out that Fakes won't create a shim for Int32 - no idea why though unfortunately. – RB. Feb 26 '14 at 13:04
  • `int` types are implemented as `struct` in the background. They cannot be inherited from or overridden easily. `CultureInfo` does not support this as the default `ToString()` method uses the `G` 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. – Kami Feb 26 '14 at 13:15

5 Answers5

1

If you create your own CultureInfo and you can alter it and then assign it to CultureInfo.CurrentCulture like in this answer:

https://stackoverflow.com/a/24785761/166921

Community
  • 1
  • 1
Kamil Szot
  • 17,436
  • 6
  • 62
  • 65
  • Good idea, but unfortunately, this doesn't help me because the `CultureInfo` is not the problem here. The correct culture is used, but the default `ToString` implementation uses the `G` format instead of the one I want (`N0`). – fero Nov 28 '16 at 07:40
  • @fero I'm afraid that's not customizable. It's always `G`. And `CultureInfo` doesn't have much influence over `G` format (you can change separator but you can't change number of decimal places). – Kamil Szot Nov 28 '16 at 14:17
0

You Can override systems toString() method into your class as under:

 public override string ToString()
    {
    int i = 123456;
        string s = "Hello " + i;
      return string.Format("Hello {0}", i);
    }
Dilip Suvagiya
  • 396
  • 1
  • 4
  • 14
  • 1
    I don't have a class. I want to specify the default format for every `integer`. – fero Feb 26 '14 at 12:44
  • 2
    In addition to @fero's comment, he could not subclass `int` even if he wanted to. – RB. Feb 26 '14 at 12:48
0

you can use extension methods

public static class MyExtensions
{
    public static string ToDefaultFormatString(this int i)
    {
        //Staf
    }
}

and your code look like

int i = 123456;

string s = "Hello " + i.ToDefaultFormatString();
Ahmed Sakr
  • 151
  • 1
  • 4
  • But that's not what he's trying to achieve - he's quite clear that he's trying to control the result of ToString() on int. – RB. Feb 26 '14 at 12:50
0

As you are trying to modify the functionality for a primitive type, which has no class, you cannot override the ToString() method.

You can however create an extension method.

namespace System
{

    public class IntExt
    {
        public string ToStringN0(this int i)
        {
            return i.ToString("N0");
        }
    }

}

and then use by

int i = 5000;
Console.WriteLine(i.ToStringN0());

The example puts the class in the System namespace so it will be available through the application.

Kami
  • 19,134
  • 4
  • 51
  • 63
-1

This maybe help you :

Decimal.ToString Method (String) MSDN

Double.ToString Method (String) MSDN

asp.net mvc set number format default decimal thousands separators

Community
  • 1
  • 1
muhammetsahin
  • 181
  • 4
  • 14
  • 1
    No, this doesn't help me at all. I already know that the `"G"` format is used by default. But they don't say anything about specifying another default format. – fero Feb 26 '14 at 12:43