IFormatProvider with Integer value

6k Views Asked by At

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?

4

There are 4 best solutions below

0
On
0
On

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.

1
On

If you want to use Current Culture

System.Globalization.CultureInfo.CurrentCulture.NumberFormat

or ex:

new CultureInfo("en-UK").NumberFormat
0
On

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.