您的位置:首页 > 其它

Convert 类中的一些转换方法,包括 ToInt32、ToBoolean 和 ToString

2012-12-07 11:20 330 查看



[title3]
示例演示 Convert 类中的一些转换方法,包括 ToInt32、ToBoolean 和 ToString。[/title3]

分类: C# ASP.NET2011-06-30
14:39 77人阅读 评论(0) 收藏 举报

[c-sharp] view
plaincopy

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));

分享到:

上一篇:asp.net中System.DateTime.Now.ToString()的一些用法
下一篇:记住哪里是单引号哪里是双引号




我看到c#中有.tostring。没有toint么

string s="33";

int i=Convert.toint32(s);

//可以实现转换。我问的是为什么不可以

int i=s.toint();

ToString(num, 8) 以8进制输出字符串

如:num = 10 结果:"12"

ToInt64(num, 2) //num的格式为2进制要转为整数

如:num = "1111" 结果 15

int Num= Convert.ToInt32(txtPNum.Text);

int Price=Convert.ToInt32(txtPrice.Text) ;

txtAllPrice.Text=(Price*Num).ToString();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐