Determining if a String is a Valid Number by Parse functions : Number : Data Types C# Source Code


Custom Search

C# Source Code » Data Types » Number »

 

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
      }

        }
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Data Types
» Number