110,534
社区成员
发帖
与我相关
我的任务
分享
string misc = "36.987";
string mis = "0";
double dbu = Convert.ToDouble(misc);
bool boolean = Convert.ToBoolean(dbu);
bool boomis = Convert.ToBoolean(mis); //这里出错
int cnt = Convert.ToInt32(misc); //这里也出错
Console.WriteLine("字符串转换为浮点型:dbu = {0}\n", dbu);
Console.WriteLine("字符串转换为布尔型:boolean = {0}\n", boolean);
Console.WriteLine("字符串转换为整型:cnt = {0}\n", cnt);
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.
[/quote]
没仔细看MSDN文档,谢谢了
嗯,基本理解了,谢谢指点了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.
string misc = "36.987";
double dbu = Convert.ToDouble(misc);
bool boolean = Convert.ToBoolean(dbu);
却是正确的,请问是什么情况呢