简体   繁体   English

将十进制转换为双精度返回科学计数法

[英]Convert decimal to double returns scientific notation

I am reading data from an Excel file and the data value and the value is 0.0000000001.我正在从 Excel 文件中读取数据,数据值为 0.0000000001。 When it is read from C# the value is returned as 1E-10.从 C# 读取时,该值返回为 1E-10。 The cell value is row.Cells[2].Value = 1E-10单元格值为row.Cells[2].Value = 1E-10

I tried from other posts to first convert to a decimal:我尝试从其他帖子中首先转换为小数:

decimal h1 = Decimal.Parse(row.Cells[2].Value.ToString(), System.Globalization.NumberStyles.Any);

and here h1 = 0.0000000001 as expected.这里h1 = 0.0000000001正如预期的那样。

I need the value to be in double so I then did:我需要双倍的价值,所以我做了:

Double val1 = Decimal.ToDouble(h1);

But val1 is now back to 1E-10val1现在又回到 1E-10

How can I keep the value as double = 0.0000000001?如何将值保持为 double = 0.0000000001?

Please test the following snippet of code to illustrate what the issue is:请测试以下代码片段以说明问题所在:

decimal t1 = Decimal.Parse("1E-10", System.Globalization.NumberStyles.Any);十进制 t1 = Decimal.Parse("1E-10", System.Globalization.NumberStyles.Any); Double t2 = Convert.ToDouble(t1);双 t2 = Convert.ToDouble(t1);

You will see t1 =.0000000001 and t2 = 1E-10您将看到 t1 =.0000000001 和 t2 = 1E-10

1E-10 is the same as 0.0000000001 1E-100.0000000001相同

I have not tested it but maybe this will help:我还没有测试过,但也许这会有所帮助:

string variable = $"{val1:N10}";

Use the default conversion class使用默认转换 class

 Double val1 = Convert.ToDouble(h1)

And convert to string to represent the full number并转换为字符串来表示完整的数字

Console.WriteLine(h1.toString("F10"))

Where F10 it's the decimales format what you want F10 是你想要的小数格式

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

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