using System; using System.Data; using System.Text.RegularExpressions; class Class1{ static void Main(string[] args){ string IsNotNum = "111west"; string IsNum = " +111 "; string IsFloat = " 23.11 "; string IsExp = " +23 e+11 "; Console.WriteLine(IsIntegerRegEx(IsNum)); // True Console.WriteLine(IsIntegerRegEx(IsNotNum)); // False Console.WriteLine(IsIntegerRegEx(IsFloat)); // False Console.WriteLine(IsIntegerRegEx(IsExp)); // False } public static bool IsIntegerRegEx(string str) { str = str.Trim(); return (Regex.IsMatch(str, @"^[+-]?d+$")); } }
Month: February 2011
Use int.Parse to check: is it an integer
using System; using System.Data; class Class1{ static void Main(string[] args){ string IsNotNum = "111west"; string IsNum = " +111 "; string IsFloat = " 23.11 "; string IsExp = " +23 e+11 "; Console.WriteLine(IsNumeric(IsNum)); // True Console.WriteLine(IsNumeric(IsNotNum)); // False Console.WriteLine(IsNumeric(IsFloat)); // False Console.WriteLine(IsNumeric(IsExp)); // False Console.WriteLine(); } public static bool IsNumeric(string str) { try { str = str.Trim(); int foo = int.Parse(str); return (true); } catch (FormatException e) { Console.WriteLine("Not a numeric value: {0}", e.ToString()); return (false); } } }
Use regualr expression to check: is it a number
using System; using System.Data; using System.Text.RegularExpressions; class Class1{ static void Main(string[] args){ string IsNotNum = "111west"; string IsNum = " +111 "; string IsFloat = " 23.11 "; string IsExp = " +23 e+11 "; Console.WriteLine(IsNumericRegEx(IsNum)); // True Console.WriteLine(IsNumericRegEx(IsNotNum)); // False Console.WriteLine(IsNumericRegEx(IsFloat)); // True Console.WriteLine(IsNumericRegEx(IsExp)); // False } public static bool IsNumericRegEx(string str) { str = str.Trim(); return (Regex.IsMatch(str, @"^[+-]?d*.?[Ee]?[+-]?d*$")); } }
Determining if a String is a Valid Number by Parse functions
using System; using System.Data; class Class1{ static void Main(string[] args){ string IsNotNum = "111west"; string IsNum = " +111 "; string IsFloat = " 23.11 "; string IsExp = " +23 e+11 "; Console.WriteLine(int.Parse(IsNum)); Console.WriteLine(float.Parse(IsNum)); // 111 Console.WriteLine(float.Parse(IsFloat)); // 23.11 //Console.WriteLine(float.Parse(IsExp)); // throws try { Console.WriteLine(int.Parse(IsNotNum)); } catch (FormatException e) { Console.WriteLine("Not a numeric value: {0}", e.ToString()); // throws } } }
Converting a Number in Another Base to Base10
using System; using System.Data; class Class1{ static void Main(string[] args){ string base2 = "111"; string base8 = "117"; string base10 = "1110"; string base16 = "11F1FF"; Console.WriteLine("Convert.ToInt32(base2, 2) = " + Convert.ToInt32(base2, 2)); Console.WriteLine("Convert.ToInt32(base8, 8) = " + Convert.ToInt32(base8, 8)); Console.WriteLine("Convert.ToInt32(base10, 10) = " + Convert.ToInt32(base10, 10)); Console.WriteLine("Convert.ToInt32(base16, 16) = " + Convert.ToInt32(base16, 16)); } }
Converting Radians to Degrees
using System; using System.Data; class Class1{ static void Main(string[] args){ Console.WriteLine(ConvertRadiansToDegrees (2)); Console.WriteLine(ConvertRadiansToDegrees (3.1415)); Console.WriteLine(ConvertRadiansToDegrees (0)); } public static double ConvertRadiansToDegrees(double radians) { double degrees = (180 / Math.PI) * radians; return (degrees); } }
Converting Radians to Degrees
using System; using System.Data; class Class1{ static void Main(string[] args){ Console.WriteLine(ConvertRadiansToDegrees (2)); Console.WriteLine(ConvertRadiansToDegrees (3.1415)); Console.WriteLine(ConvertRadiansToDegrees (0)); } public static double ConvertRadiansToDegrees(double radians) { double degrees = (180 / Math.PI) * radians; return (degrees); } }