Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Int32.ToString() culture-specific?

I'm running a beta version of ReSharper, and it's giving me warnings for the following code:

int id; // ... DoSomethingWith(id.ToString()); 

The warning is on the id.ToString() call, and it's telling me "Specify a culture in string conversion explicitly". I understand the warning, and I know how to fix it -- just change the code to the much more unwieldy id.ToString(CultureInfo.InvariantCulture).

But my question is: is that necessary? I mean, obviously it's important to specify the culture when you're using types like DateTime (different cultures have different date formats) and Double (different characters used for the decimal point). But Int32.ToString(), at least in the en-US and invariant cultures, doesn't add any formatting at all. No commas, no decimal points, no dollar signs, nothing. So what would there be to vary by culture?

Are there some cultures that actually add some sort of formatting when you call the parameterless Int32.ToString()? Or is this a bug in the ReSharper beta, and this warning really isn't applicable to Int32 (in which case I'll file a ReSharper bug report)?

like image 639
Joe White Avatar asked Dec 13 '11 16:12

Joe White


People also ask

What is ToString () and string format ()?

string format - uses under the hood- StringBuilder - which is much faster for working with strings. ToString is the default representation of an object. Follow this answer to receive notifications.

What is the ToString in C#?

ToString is the major formatting method in the . NET Framework. It converts an object to its string representation so that it is suitable for display.

How do I use IFormatProvider?

The class that implements IFormatProvider can then be used in a call to a formatting and parsing operation. For example, the following code calls the String. Format(IFormatProvider, String, Object[]) method to generate a string that contains a formatted 12-digit account number. using System; using System.


2 Answers

The Operating System allows to change the negative sign for numbers.

Control panel ->     Language and regional settings ->           Additional settings ->               Negative sign 

So, current culture could have overridden the negative sign. In this case you need to respect the regional settings, this is the reason of the warning. You can also change the negative sign programatically:

    CultureInfo culture = Thread.CurrentThread.CurrentCulture;     // Make a writable clone     culture = (CultureInfo) culture.Clone();     culture.NumberFormat.NegativeSign = "!"; 
like image 190
Daniel Peñalba Avatar answered Oct 01 '22 21:10

Daniel Peñalba


As tested on a random sample of ints, all 352 cultures installed with Windows (CultureTypes.InstalledWin32Cultures) give identical results.

Daniel is right to note a custom culture could use a different prefix for negative numbers, but I doubt anyone has ever used this feature save by accident.

I guess the .NET devs did it to be consistent with float and other types. What else were they expecting?

> int.MaxValue.ToString(CultureInfo.AncientRome) MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.... 
like image 24
Colonel Panic Avatar answered Oct 01 '22 21:10

Colonel Panic