C#不是C++
数字类型不能转换为bool类型
小数也不能转换为整数如果想小数转整数,你需要先转double,double再转int

解决方案 »

  1.   

    数值类型互相转换是没有问题的
    需要注意的就是bool类型和string类型
    不是任何string都能转成别的类型的
    比如"XYZ"这个字符串,你不管转成什么数值类型,不管按什么进制转换,都是转不了的.
    而bool类型,ToString之后,是"True"或"False",而不是"1"和"0".
      

  2.   

    数字类型不能转换为bool类型
    但是这样
    string misc = "36.987";
    double dbu = Convert.ToDouble(misc);
    bool boolean = Convert.ToBoolean(dbu);
    却是正确的,请问是什么情况呢
      

  3.   

    好吧,是我记错了.
    数值可以转bool,0是false,不等于0是true不过"0"是个字符串,你从字符串直接转bool不行.
    要么就写成"False"或"True",也是可以转的
      

  4.   

    string是字符串,你不能要求任意字符串都能转成数值.
    必须是符合数值规则的字符串才能转成对应类型的数值
    然后数值之间你就可以任意转了.
      

  5.   

    它允许从任何基本数据类型转换到其它基本数据类型
    书上这么说,是说错了,还是我哪里理解错了吗?求指点
    确实是任意类型都可以互相转啊,不过对于数据有要求,数据本身必须能转,才能转.数据本身不是那个格式的,就转不了.
    string确实可以转int或double或bool
    不过必须是整数才能转int,小数转double,true或false转bool
    不是随便写个XYZ就能转成任意数字了
      

  6.   


    不能“想当然”,要根据库提供的函数和参数操作,否则就出错。
    Convert.ToBoolean 方法 (String)
    参数必须能被它的规则识别才行,而不是0和1就行的。
    using System;public class BooleanConversion
    {
       public static void Main()
       {
          String[] values = { null, String.Empty, "true", "TrueString", 
                              "False", "    false    ", "-1", "0" };
          foreach (var value in values) {
             try
             {
                Console.WriteLine("Converted '{0}' to {1}.", value,  
                                  Convert.ToBoolean(value));
             }
             catch (FormatException)
             {
                Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
             }
          }   
       }
    }
    // The example displays the following output:
    //       Converted '' to False.
    //       Unable to convert '' to a Boolean.
    //       Converted 'true' to True.
    //       Unable to convert 'TrueString' to a Boolean.
    //       Converted 'False' to False.
    //       Converted '    false    ' to False.
    //       Unable to convert '-1' to a Boolean.
    //       Unable to convert '0' to a Boolean.
      

  7.   


    不能“想当然”,要根据库提供的函数和参数操作,否则就出错。
    Convert.ToBoolean 方法 (String)
    参数必须能被它的规则识别才行,而不是0和1就行的。
    using System;public class BooleanConversion
    {
       public static void Main()
       {
          String[] values = { null, String.Empty, "true", "TrueString", 
                              "False", "    false    ", "-1", "0" };
          foreach (var value in values) {
             try
             {
                Console.WriteLine("Converted '{0}' to {1}.", value,  
                                  Convert.ToBoolean(value));
             }
             catch (FormatException)
             {
                Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
             }
          }   
       }
    }
    // The example displays the following output:
    //       Converted '' to False.
    //       Unable to convert '' to a Boolean.
    //       Converted 'true' to True.
    //       Unable to convert 'TrueString' to a Boolean.
    //       Converted 'False' to False.
    //       Converted '    false    ' to False.
    //       Unable to convert '-1' to a Boolean.
    //       Unable to convert '0' to a Boolean.

    没仔细看MSDN文档,谢谢了嗯,基本理解了,谢谢指点了