Answers for "C# try parse int to string"

C#
0

C# try parse int to string

using System;

public class Example
{
   public static void Main()
   {
      String[] values = { null, "160519", "9432.0", "16,667",
                          "   -322   ", "+4302", "(100);", "01FA" };
      foreach (var value in values)
      {
         int number;

         bool success = Int32.TryParse(value, out number);
         if (success)
         {
            Console.WriteLine("Converted '{0}' to {1}.", value, number);
         }
         else
         {
            Console.WriteLine("Attempted conversion of '{0}' failed.",
                               value ?? "<null>");
         }
      }
   }
}
// The example displays the following output:
//       Attempted conversion of '<null>' failed.
//       Converted '160519' to 160519.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100);' failed.
//       Attempted conversion of '01FA' failed.
Posted by: Guest on August-30-2021

C# Answers by Framework

Browse Popular Code Answers by Language