简体   繁体   English

来自查询的双值以cml中的科学计数法出现在c#中

[英]Double values from query appear in scientific notation in xml in c#

I am modifying a code in c# that retrieves data from a query and displays it in xml. 我正在修改c#中的代码,该代码从查询中检索数据并将其显示在xml中。 Actually, I am modifying the query, since the rest of the code should not change. 实际上,我正在修改查询,因为其余代码不应该更改。

This query returns a column which is sometimes a decimal number (precision 2), and other times an integer. 此查询返回一个列,有时是十进制数(精度2),有时是整数。 Executing the query itself gives the correct results. 执行查询本身会得到正确的结果。

The data from the query are parsed by the code below to be served as xml. 来自查询的数据由以下代码解析,以作为xml提供。 However in the xml document what I get are scientific notation values for the entire column. 但是在xml文档中,我得到的是整个列的科学记数值。

The code: 编码:

database_connection = new SqlConnection(CONNECTION_STRING); 
command = new System.Data.SqlClient.SqlCommand(sql_query, database_connection);
command.Connection.Open();
document = new System.Xml.XmlDocument();
document.Load(command.ExecuteXmlReader());

list = document.GetElementsByTagName("time_in_months");

foreach (XmlNode elementToEncrypt in list)
{
     elementToEncrypt.InnerXml = elementToEncrypt.InnerXml;
}
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
document.Save(Response.Output);
command.Connection.Close();
Response.End();

Basically, the query returns for ex. 基本上,查询返回ex。 0.33 for the db value 10 (10 days = 0.33 months) and 3 for the db value 90. db值为10(10天= 0.33个月)为0.33,db值为90为3。

The xml document though returns 3.300000000000000e-001 for 10 days and 3.000000000000000e+000 for 90 days. xml文档虽然在10天内返回3.300000000000000e-001而在90天内返回3.000000000000000e + 000。

I could set a standard that all values be double, but the scientific notation is a problem. 我可以设定一个标准,所有值都是双倍的,但科学记数法是一个问题。 Not being ac# programmer, I really am not sure where the conversation of types happens. 不是ac#程序员,我真的不确定类型的对话在哪里发生。

PS: I am aware that days of the month vary, but this does not matter in the way we are doing the calculations. PS:我知道这个月的日子各不相同,但这与我们进行计算的方式无关。 We are taking it as if each month has 30 days. 我们假设每个月都有30天。

I haven't figured this out completely, so other answers are welcomed, but I did solve the problem. 我没有完全弄清楚这一点,所以欢迎其他答案,但我确实解决了这个问题。

I cast the values at the query as varchar (with reasonable length) and now the values are displayed properly. 我将查询中的值转换为varchar(具有合理的长度),现在值正确显示。 The query execution still gives the same results, but the xml data are formatted properly. 查询执行仍然提供相同的结果,但xml数据格式正确。

Just a wild guess, ma be that when the data are parsed into xml, it detects two different datatypes and converts them to scientific notation to support both datatypes. 只是一个疯狂的猜测,可能是当数据被解析为xml时,它会检测两种不同的数据类型并将它们转换为科学计数法以支持这两种数据类型。

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

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