English 中文(简体)
如何将扼杀物清单翻一番?
原标题:How to convert list of strings to doubles?

——

List<double> doubleList =
stringList.ConvertAll(x => (double)x);

List<double> doubleList =
stringList.Select(x =>
(double)x).ToList();

并且得出这一错误:

不能将体力转换成倍数

我读了similiar 。 将斜体改为两倍......但我有需要改写成双倍名单的插图清单,而ConvertAll()则不使用选择性的延伸方法。 任何人都可以帮助我。

最佳回答

选择的方法如果是使用的话,就应当发挥作用。 NET 3.5或更新的:

List<double> result = l.Select(x => double.Parse(x)).ToList();

举例说:

List<string> l = new List<string> { (0.1).ToString(), (1.5).ToString() };
List<double> result = l.Select(x => double.Parse(x)).ToList();
foreach (double x in result)
{
    Console.WriteLine(x);
}

结果:

0,1
1,5

了解的一件事是,你利用哪一种文化来压制这些扼杀。 您不妨使用<代码>Parse超载荷,这些超载带文化和使用<代码>CultureInfo.InvariantCulture。

问题回答

您可以使用支线:

List<double> myList = myStringlist.ConvertAll(item => double.Parse(item));

3. 请认识到,双管齐下和浮动是复杂的——只是想:

100,00 100.00

- 设计;

您可使用名单的每个方法

List<double> dbl= new List<double>;
stringList.ForEach( str=> dbl.Add( double.parse( str ) ) );

如何做到这一点?

List<string> list = [your strings]
List<double> newList = new List<double>();
for(int i = 0; i < list.Count; i++)
{
  double d = 0;
  if(!double.TryParse(list[i], d)) //Error
  newList.Add(d);
}

Hope this may work: List tmpDouble = tmpString.Select(x => (double ?)Convert.ToDouble (x)).ToList();





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签