4

I have an Integer value in my object. I need to cast it as an integer value. So I have done it this way. System.Convert.ToInt64(Object) But FxCop said that I need to provide with IFormatProvider. String data type I have no issue with provide IFormatProvider. How can I provide an IFormatProvider for integer value?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Charitha
  • 517
  • 2
  • 7
  • 20

4 Answers4

6

It depends on how you need to print your value.

e.g. using:

var provider = System.Globalization.CultureInfo.InvariantCulture;

you will get a string that is independent from your local (regional) settings.

Using:

var provider = System.Globalization.CultureInfo.CurrentCulture;

or:

var provider = System.Globalization.CultureInfo.CurrentUICulture;

instead, the string will be printed using your local (regional) machine settings.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
2

If you want to use Current Culture

System.Globalization.CultureInfo.CurrentCulture.NumberFormat

or ex:

new CultureInfo("en-UK").NumberFormat
Vijay Sirigiri
  • 4,653
  • 29
  • 31
  • Generates error: `new CultureInfo("en-UK").NumberFormat 'new CultureInfo("en-UK")' threw an exception of type 'System.ArgumentException' System.Globalization.NumberFormatInfo`.... It should be like this: `new CultureInfo("en-GB").NumberFormat` or `new CultureInfo("en-US").NumberFormat` – uzay95 Jan 27 '12 at 15:33
1

see here: IFormatProvider Interface

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

Is there a problem with just casting the object variable?

Int64 i = (Int64) myObject;

If it really is just a boxed integer, I don't see why that wouldn't work.

user
  • 6,897
  • 8
  • 43
  • 79