【问题标题】:C# convert string array output to doubleC# 将字符串数组输出转换为双精度
【发布时间】:2014-12-19 09:35:20
【问题描述】:

我正在尝试从数组中获取输出并转换为双精度以用于计算。

这就是我想要做的:

Console.WriteLine(product[1]);
double units = Convert.ToDouble(Console.ReadLine());

一直在尝试其他一些事情,但无处可去;有什么简单的解决办法吗?

【问题讨论】:

  • 您希望这段代码做什么?而它的作用是什么?
  • 尝试双倍单位 = Convert.ToDouble(product[1]); Console.WriteLine(单位);

标签: c# arrays string double output


【解决方案1】:

没有必要把它写到控制台再读回来..简单:

var units = Convert.ToDouble(product[1]);

您也可以考虑使用 Double.TryParse() 来检查该值是否可以转换为双精度并且不是字母字符串。

【讨论】:

  • 感谢 Dan 和大家 - 以上对我有用
【解决方案2】:

如果用户键入一些无效的双精度,您的行可能会引发异常

double units = Convert.ToDouble(Console.ReadLine());

你应该这样做

double units ;
if (!double.TryParse(Console.ReadLine(), out units )) {
    //units is not a double
}
else{
  //units is a double
}

【讨论】:

    【解决方案3】:

    如果您需要将整个数组转换为双精度数,您可以这样做:

    using System.Linq;
    
    var doubleProduct = product.Select(p => double.Parse(p)).ToArray();
    

    编辑

    您也可以使用Array.ConvertAll(),这显然更有效(感谢@PetSerAl 的提示)。这也意味着您不需要 Linq:

    var doubleProduct = Array.ConvertAll(product, p => double.Parse(p));
    

    【讨论】:

    • 你可以使用Array.ConvertAll来提高数组的效率。
    • @PetSerAl 感谢您的提示!我会更新答案。
    【解决方案4】:
    using System;
    
    public class Example
    {
       public static void Main()
       {
          string[] values= { "-1,035.77219", "1AFF", "1e-35", 
                             "1,635,592,999,999,999,999,999,999", "-17.455", 
                             "190.34001", "1.29e325"};
          double result;
    
          foreach (string value in values)
          {
             try {
                result = Convert.ToDouble(value);
                Console.WriteLine("Converted '{0}' to {1}.", value, result);
             }   
             catch (FormatException) {
                Console.WriteLine("Unable to convert '{0}' to a Double.", value);
             }               
             catch (OverflowException) {
                Console.WriteLine("'{0}' is outside the range of a Double.", value);
             }
          }       
       }   
    }
    // The example displays the following output:
    //       Converted '-1,035.77219' to -1035.77219.
    //       Unable to convert '1AFF' to a Double.
    //       Converted '1e-35' to 1E-35.
    //       Converted '1,635,592,999,999,999,999,999,999' to 1.635593E+24.
    //       Converted '-17.455' to -17.455.
    //       Converted '190.34001' to 190.34001.
    //       '1.29e325' is outside the range of a Double.
    

    阅读MSDN

    Console.WriteLine Method (String, Object)

    Console.ReadLine Method

    【讨论】:

      【解决方案5】:

      请尝试以下操作:

      using System;
      
      public class Program
      {
          public static void Main()
          {
              string[] products= { "10.5","20.5","50.5"};
              foreach (var product in products)
              {
                   Console.WriteLine(Convert.ToDouble(product));
              }           
          }
      }
      

      Live Demo

      【讨论】:

        猜你喜欢
        • 2013-10-10
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        • 2014-01-01
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多