CultureInfo

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi

I have run "FxCop" on one of my programs, and got a lot of warnings
about using int.ToString() without a "format provider".

The help for this warning says I can use a CultureInfo as a
FormatProvider. But what difference can this make?

For example, if I have the integer
int i = 123456789;

What CultureInfo object will give a different result than "123456789"
for
string s = i.ToString(cultureInfo);

Thanks,
Peter
 
Peter said:
What CultureInfo object will give a different result than "123456789"
for
string s = i.ToString(cultureInfo);

Perhaps, for instance, one that issues Arabic digits?

--
Rudy Velthuis http://rvelthuis.de

"Alas, to wear the mantle of Galileo it is not enough that you
be persecuted by an unkind establishment, you must also be
right." -- Bob Park
 
Rudy said:
Perhaps, for instance, one that issues Arabic digits?

Thanks - how do I generate such a CultureInfo for testing? My system
doesn't have an "arabic" culture I can use for string formatting.

--
 
Peter said:
Thanks - how do I generate such a CultureInfo for testing? My system
doesn't have an "arabic" culture I can use for string formatting.

After a little more reading and thinking I guess it doesn't matter in
my case.

(1) The chance that my program ever runs in a system utilising a
CultureInfo where this could fail is 0.

(2) The values I am converting between string and int and back again
come from an xml file - and here I think it's safe to assume I can use
CultureInfo.Invariant culture in any case.

/Peter
 
I have run "FxCop" on one of my programs, and got a lot of warnings
about using int.ToString() without a "format provider".

The help for this warning says I can use a CultureInfo as a
FormatProvider. But what difference can this make?

For example, if I have the integer
int i = 123456789;

What CultureInfo object will give a different result than "123456789"
for
string s = i.ToString(cultureInfo);

Just a thought, but perhaps the issue is that different cultures use
different thousands separators? I know that in en-US, if you don't ask for a
thousands separator in int.ToString() then you won't get one, but maybe it's
different for other cultures. But I'm far from a globalization expert....
 
Just a thought, but perhaps the issue is that different cultures use
different thousands separators? I know that in en-US, if you don't ask for a
thousands separator in int.ToString() then you won't get one, but maybe it's
different for other cultures. But I'm far from a globalization expert....


We were bit by this once, converting a value that we stored in an xml
file.

The value was something like 1.5 and when we converted on a system
that was using a culture with a different decimal separator, the value
came out as 15. Not quite the same number... :)

Marc
http://nomagichere.blogspot.com
 
We were bit by this once, converting a value that we stored in an xml
file.
The value was something like 1.5 and when we converted on a system
that was using a culture with a different decimal separator, the value
came out as 15. Not quite the same number... :)

I can definitely see it happening with decimals, but we're talking about
ints here. That's why my reply was worded as a "maybe."
 
Peter said:
I have run "FxCop" on one of my programs, and got a lot of warnings
about using int.ToString() without a "format provider".

The help for this warning says I can use a CultureInfo as a
FormatProvider. But what difference can this make?

For example, if I have the integer
int i = 123456789;

What CultureInfo object will give a different result than "123456789"
for
string s = i.ToString(cultureInfo);
The point of the warning is to force you to specify what CultureInfo you're
going for, so it's easier to spot globalization problems in the code. In
your case, you probably want

string s = i.ToString(CultureInfo.InvariantCulture);

This signifies to everyone that this particular piece of string formatting
is not culture-dependent and should remain the same. This is important if
the data is to be processed automatically and must not suddenly get a
different format if different users run the application.

If, on the other hand, you intend this string to be presented to the user,
you'd likely go for

string s = i.ToString(CultureInfo.CurrentCulture);

Now it's clear that the results might (need not, but might) vary based on
the user's culture. Note that most .ToString() methods that do not take a
culture will use the current culture for formatting, which is not always the
right choice.

Whether some particular formatting *actually* gives different results for
different cultures is not what FxCop is checking for (that's impossible
tocheck statically). It just wants you to be explicit.
 
Back
Top