简体   繁体   English

如何一步将xsd:double字符串转换为十进制

[英]How to convert xsd:double string to decimal in one step

I'm using MdxClient which internally parses XML documents returned by AdomdCommand.ExecuteXmlReader . 我正在使用MdxClient内部分析AdomdCommand.ExecuteXmlReader返回的XML文档。 Some of values are returned as xsd:double , but I want them as decimal at client side. 一些值以xsd:double形式返回,但是我希望它们在客户端为decimal

This library to parse values uses Convert.ChangeType method. 该库用于解析值,使用Convert.ChangeType方法。 But some of xsd:double strings such as 3.514680845402702E1 or 4.058719395866455E1 cannot been converted to decimal : 但是某些xsd:double字符串(如3.514680845402702E14.058719395866455E1无法转换为decimal

var result = Convert.ChangeType("3.514680845402702E1", typeof(decimal), CultureInfo.InvariantCulture);

throws FormatException . 抛出FormatException

I know I can convert it in two steps: 我知道我可以分两步进行转换:

var tmp = Convert.ChangeType("3.514680845402702E1", typeof(double), CultureInfo.InvariantCulture);
var result2 = Convert.ChangeType(tmp, typeof(decimal), CultureInfo.InvariantCulture);

but I'm wondering if it's possible in one step? 但我想知道是否可以一步一步完成? Maybe by providing custom IFormatProvider implementation as third argument? 也许通过提供自定义IFormatProvider实现作为第三个参数? Any ideas? 有任何想法吗?

Do you have to use Convert.ChangeType(...) ? 您必须使用Convert.ChangeType(...)吗?

If you simply want to convert a string containing a number formatted in Exponential Notation, you can do the following: 如果只想转换包含以指数表示法格式的数字的字符串,则可以执行以下操作:

var result = decimal.Parse("3.514680845402702E1", System.Globalization.NumberStyles.Float);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM