如何将对象更改为double [英] How do I change an object to a double
本文介绍了如何将对象更改为double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataTable有一个输出作为对象。
我想将它转换为Double。
这些给出了Cast错误:
A DataTable has an output as an Object.
I want to convert it to a Double.
These gave a Cast error:
Convert.ToDouble(dt.Rows[i][dc]);
(double)dt.Rows[i][dc];
有没有办法将对象更改为Double?
Is there any way of changing an Object to a Double?
double[,] f = new double[10000,10000];
foreach (DataColumn dc in dt.Columns)
{
h = h + 1;
for (int i = 0; i<=1500; i++)
{
f[h, i] = Convert.ToDouble(dt.Rows[i][dc]); }
}
}
我尝试了什么:
What I have tried:
Convert.ToDouble(dt.Rows[i][dc]);
(double)dt.Rows[i][dc];
推荐答案
以下代码将处理转换异常,但您必须确保该表具有有效数字
The below code will take care of casting exception, but you will have to make sure that the table has valid numbers
double[,] f = new double[10000, 10000];
foreach (DataColumn dc in dt.Columns)
{
h = h + 1;
for (int i = 0; i <= 1500; i++)
{
string value = dt.Rows[i][dc] + "";
double d = 0;
double.TryParse(value, out d);
f[h, i] = d;
}
}
dc是DataColumn类型,不是字符串或数值。所以你不能这样做:
dc is a DataColumn type, not a string or numeric value. So you can't do this:
dt.Rows[i][dc]
因为 dc
不是有效的索引表达式。
可能你想要这个:
because dc
is not a valid index expression.
Probably you want this:
(double) dt.Rows[i][dc.Ordinal]
Convert.ToDouble(dt.Rows [i] [dc] .Tostring());
这将解决你的问题+
Convert.ToDouble(dt.Rows[i][dc].Tostring());
this will solve your problem+
这篇关于如何将对象更改为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文