数值类型的转换
Parse()方法像是一座桥梁可以将纯数字组成的字符串解析为相应的数值类型!
强制类型转换与运用Convert类的方法转换的不同之处在于强制类型转换是一种截断,而Convert则是一种四舍五入的转换!
如下例所示:
using System;
class MyTestPro
{
public static void Main()
{
string str = "12345.678";
double d = double.Parse(str);
Console.WriteLine("String str -> {0}",str);
Console.WriteLine("double.Parse() -> {0}",d);
int inta = (int)d; //将小数点后的数直接舍去
Console.WriteLine("(int)d -> {0}",inta);
int intb = Convert.ToInt32(d); //将小数点后的数四舍五入
Console.WriteLine("Convert.ToInt32() -> {0}",intb);
Console.ReadLine();
}
}