示例演示 Convert 类中的一些转换方法,包括 ToInt32、ToBoolean 和 ToString。
2011-06-30 14:39
441 查看
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));
相关文章推荐
- Convert 类中的一些转换方法,包括 ToInt32、ToBoolean 和 ToString
- 【原创】C#两种任意类型转换支持(示例:String convert to Type)
- C#学习笔记8-整型的三种强制转换int、int.Parse()和Convert.ToInt32()
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String)”,因此该方法无法转换为存储表达式
- Convert.ToString 方法 (Boolean, IFormatProvider)
- ConvertJSONDateToJSDateObject 方法实现json格式时间串转换为 对应的时间格式串
- LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。
- System.NotSupportedException: LINQ to Entities 不识别方法“System.Decimal ToDecimal(Int32)”,因此该方法无法转换为存储表达
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String)”,因此该方法无法转换为存储表达式。
- JS的数组转换字符串方法toString、toLocaleString、join和valueOf — 第5.2.1节
- java格式转换的一些方法
- ToStringBuilder和ToStringStyle(覆盖每个实体的 toString 方法)
- C#转换全角半角方法示例
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
- string 至 Color 转换演示示例:
- 常用的js方法,包括表单校验的一些方法,下拉菜单常用的方法等等
- convert-sorted-list-to-binary-search-tree(转换排序列表到二进制搜索树)
- leetCode 108.Convert Sorted Array to Binary Search Tree(将排序数组转换为BST) 解题思路和方法
- Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示(待补全)
- DateTime.ToString() 方法演示