IFormatProvider with Integer value
I have an Integer value in my object. I need to cast it as an integer value. So I have do开发者_Go百科ne 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?
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.
If you want to use Current Culture
System.Globalization.CultureInfo.CurrentCulture.NumberFormat
or ex:
new CultureInfo("en-UK").NumberFormat
see here: IFormatProvider Interface
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.
精彩评论