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();
相关文章推荐
- 示例演示 Convert 类中的一些转换方法,包括 ToInt32、ToBoolean 和 ToString。
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String)”,因此该方法无法转换为存储表达式
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String)”,因此该方法无法转换为存储表达式。
- Convert.ToString 方法 (Boolean, IFormatProvider)
- System.NotSupportedException: LINQ to Entities 不识别方法“System.Decimal ToDecimal(Int32)”,因此该方法无法转换为存储表达
- JS的数组转换字符串方法toString、toLocaleString、join和valueOf — 第5.2.1节
- ConvertJSONDateToJSDateObject 方法实现json格式时间串转换为 对应的时间格式串
- LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。
- C#学习笔记8-整型的三种强制转换int、int.Parse()和Convert.ToInt32()
- Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)
- C# ToString()方法一些特殊用法集锦
- 问题:C# ToString("P");结果:c#中的常用ToString()方法总结
- Java与C#开发上的一些差异与转换方法
- UIView中的各种坐标转换convertPoint:toView OR convertPoint:fromView
- QString和char字符数组之间的转换(QTextCodec.toUnicode方法,特别注意\0的问题)
- String.CopyTo方法和java多线程的一些问题整理
- VC++ 出现错误:cannot convert from const char *' to 'LPCWSTR' 或者'const char [7]' to 'LPCWSTR' 的解决方法
- Convert.ToInt32、(int)和int.Parse三者的区别
- C#将汉字转换成全拼的拼音EcanConvertToCh帮助类 (转)
- “LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式。”