Why doesn't object have an overload that accepts IFormatProvider?

前端 未结 3 1202
情话喂你
情话喂你 2020-12-15 03:53

When converting for instance a decimal to a string, you use the CultureInfo.InvariantCulture and pass it as an IFormatProvider

3条回答
  •  悲哀的现实
    2020-12-15 04:39

    Try one of these:

    string valueString = XmlConvert.ToString(value);
    string valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
    

    XmlConvert.ToString() is made for XML, so it will keep things closer to the XML spec, such as using "true" instead of "True". However, it is also more brittle than Convert.ToString(). For example, this will throw an exception because of the UTC time:

    XmlConvert.ToString(DateTime.UtcNow)
    

    but this works:

    XmlConvert.ToString(DateTime.UtcNow, "o")
    

提交回复
热议问题