Default number format for ToString
Asked Answered
V

5

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.

Veradis answered 26/2, 2014 at 12:33 Comment(8)
can you do something with cultureInfo.NumberFormat, in the way your example url uses DateTimeFormat ?Adoptive
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.Veats
@TomBrown No, this is why I posted my question. :-)Veradis
Related: https://mcmap.net/q/1474502/-override-tostringVeats
Why don't you customize CultureInfo.CurrentCulture.NumberFormat? E.g. CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = ".";Adonic
@DmitryBychenko That's not possible. InvalidOperationException: Instance is read-only.Veats
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.Erectile
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.Frady
D
1

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

https://mcmap.net/q/961098/-trying-to-set-the-decimal-separator-for-the-current-language-getting-quot-instance-is-read-only-quot

Decapod answered 26/11, 2016 at 1:35 Comment(2)
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).Veradis
@Veradis 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).Decapod
N
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);
    }
Neumark answered 26/2, 2014 at 12:43 Comment(2)
I don't have a class. I want to specify the default format for every integer.Veradis
In addition to @fero's comment, he could not subclass int even if he wanted to.Erectile
I
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();
Impromptu answered 26/2, 2014 at 12:48 Comment(1)
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.Erectile
F
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.

Frady answered 26/2, 2014 at 12:50 Comment(0)
A
-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

Accouter answered 26/2, 2014 at 12:40 Comment(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.Veradis

© 2022 - 2024 — McMap. All rights reserved.