Int.Parse(string);
或者
Convert.ToInt32(string)
或者
Convert.ToInt32(string)
解决方案 »
- 用VS2010开发C#界面
- C#如何获取QQ安装路径 ?并且让其获取textbox的值进行登陆?
- Thread类没有继承CriticalFinalizerObject, _Thread。所以里面少了好多方法。ResetAbort,Suspend等,是保护文件自己又不能改,怎么办呢?
- 前台和后台代码的协调问题??帮顶有分!
- 关于 数据匹配的问题 真心求教
- c#如何在网页上面播放数据库里面的音乐列表
- VRmap在C#下的编程问题,急!
- 请问一下一个学校计算机机房的配置方案
- 关于矩阵计算为什么要进行normalize()?
- -= 就要走了=-
- 不能登陆数据库?
- 如何在c#中把int转化为string?
Convert 成员 | System 命名空间 | Object | SByte | Int16 | Int32 | Int64 | Byte | UInt16 | UInt32 | UInt64 | Single | Double | Decimal | Boolean | Char | String
要求
命名空间: System平台: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family程序集: Mscorlib (在 Mscorlib.dll 中)
语言
C#C++JScriptVisual Basic全部显示
将一个基本数据类型转换为另一个基本数据类型。有关此类型所有成员的列表,请参阅 Convert 成员。System.Object
System.Convert[Visual Basic]
NotInheritable Public Class Convert
[C#]
public sealed class Convert
[C++]
public __gc __sealed class Convert
[JScript]
public class Convert
线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。备注
该类返回值与指定类型的值等效的类型。受支持的基类型是 Boolean、Char、SByte、Byte、Int16、Int32、Int64、UInt16、UInt32、UInt64、Single、Double、Decimal、DateTime 和 String。存在将每个基类型转换为每个其他基类型的转换方法。不过,所执行的实际转换操作分为三类: 从某类型到它本身的转换只返回该类型。不实际执行任何转换。
无法产生有意义的结果的转换引发 InvalidCastException。不实际执行任何转换。下列转换会引发异常:从 Char 转换为 Boolean、Single、Double、Decimal 或 DateTime,以及从这些类型转换为 Char。下列转换会引发异常:从 DateTime 转换为除 String 之外的任何类型,以及从任何类型(String 除外)转换为 DateTime。
任何基类型(上面描述的基类型除外)都可以与任何其他基类型进行相互转换。
如果 numeric 类型转换导致精度丢失(即某些最低有效位丢失),不引发异常。但是,如果结果超出了特定转换方法的返回值类型所能表示的范围,则将引发异常。例如,当将 Double 转换为 Single 时,可能会发生精度丢失,但并不引发异常。但是,如果 Double 的值太大,无法由 Single 表示,则将引发溢出异常。有一组方法可支持字节数组与 String 或由以 64 为基的数字字符组成的 Unicode 字符数组之间的转换。表示为以 64 为基的数字的数据可以很容易地通过只能传输 7 位字符的数据信道进行传送。该类中的许多方法通过调用源对象上的对应 IConvertible 显式接口实现方法,将源对象转换为目标对象。如果不存在这样的方法,则将引发 InvalidCastException。该类中的一些方法带一个实现 IFormatProvider 接口的参数对象。该参数可以提供区域性特定的格式设置信息以帮助转换过程。基值类型忽略该参数,但任何实现 IConvertible 的用户定义类型可以考虑使用它。有关基值类型的更多信息,请参阅“请参阅”一节中列出的相应主题。示例
[Visual Basic, C#] 下面的示例说明该类中的某些转换方法。[Visual Basic]
Dim dNumber As Double
dNumber = 23.15Try
' Returns 23
Dim iNumber As Integer
iNumber = System.Convert.ToInt32(dNumber)
Catch exp As System.OverflowException
System.Console.WriteLine("Overflow in double to int conversion.")
End Try' Returns True
Dim bNumber As Boolean
bNumber = System.Convert.ToBoolean(dNumber)' Returns "23.15"
Dim strNumber As String
strNumber = System.Convert.ToString(dNumber)Try
' Returns '2'
Dim chrNumber As Char
chrNumber = System.Convert.ToChar(strNumber.Chars(1))
Catch exp As System.ArgumentNullException
System.Console.WriteLine("String is null.")
Catch exp As System.FormatException
System.Console.WriteLine("String length is greater than 1.")
End Try' System.Console.ReadLine() returns a string and it
' must be converted.
Dim newInteger As Integer
newInteger = 0
Try
System.Console.WriteLine("Enter an integer:")
newInteger = System.Convert.ToInt32(System.Console.ReadLine())
Catch exp As System.ArgumentNullException
System.Console.WriteLine("String is null.")
Catch exp As System.FormatException
System.Console.WriteLine("String does not consist of an " + _
"optional sign followed by a series of digits.")
Catch exp As System.OverflowException
System.Console.WriteLine("Overflow in string to int conversion.")
End TrySystem.Console.WriteLine("Your integer as a double is {0}", _
System.Convert.ToDouble(newInteger))
[C#]
double dNumber = 23.15; try {
// Returns 23
int iNumber = System.Convert.ToInt32(dNumber);
}
catch (System.OverflowException) {
System.Console.WriteLine(
"Overflow in double to int conversion.");
}
// Returns True
bool bNumber = System.Convert.ToBoolean(dNumber);
// Returns "23.15"
string strNumber = System.Convert.ToString(dNumber); try {
// Returns '2'
char chrNumber = System.Convert.ToChar(strNumber[0]);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("String is null");
}
catch (System.FormatException) {
System.Console.WriteLine("String length is greater than 1.");
} // System.Console.ReadLine() returns a string and it
// must be converted.
int newInteger = 0;
try {
System.Console.WriteLine("Enter an integer:");
newInteger = System.Convert.ToInt32(
System.Console.ReadLine());
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("String is null.");
}
catch (System.FormatException) {
System.Console.WriteLine("String does not consist of an " +
"optional sign followed by a series of digits.");
}
catch (System.OverflowException) {
System.Console.WriteLine(
"Overflow in string to int conversion.");
} System.Console.WriteLine("Your integer as a double is {0}",
System.Convert.ToDouble(newInteger));
TheAres(班门斧) 说的简单,命中要害!就那么一点点,呵呵!
try
{
i = Convert.ToInt32(YourString);
}
catch
{
// 该字符串不能转换成整数
}
int i = (int) s;