C# Int16 TryParse(String, NumberStyles, IFormatProvider, Int16)

Description

Int16 TryParse(String, NumberStyles, IFormatProvider, Int16) converts the string representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) has the following syntax.


public static bool TryParse(
  string s,//from   w  ww  .  j ava  2 s .  c o  m
  NumberStyles style,
  IFormatProvider provider,
  out short result
)

Parameters

Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) has the following parameters.

  • s - A string containing a number to convert. The string is interpreted using the style specified by style.
  • style - A bitwise combination of enumeration values that indicates the style elements that can be present in s. A typical value to specify is NumberStyles.Integer.
  • provider - An object that supplies culture-specific formatting information about s.
  • result - When this method returns, contains the 16-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in a format compliant with style, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Returns

Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) method returns true if s was converted successfully; otherwise, false.

Example

The following example calls the Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) method with a number of different string values.


using System;//w ww.  ja v a 2 s  . co  m
using System.Globalization;

public class StringParsing
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;

      numericString = "12345";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);

      numericString = "-1234";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);

      numericString = "12345.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "12345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "12,234"; 
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(numericString, styles);

      numericString = "12E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 

      numericString = "12E03";
      CallTryParse(numericString, styles); 

      numericString = "80c1";
      CallTryParse(numericString, NumberStyles.HexNumber);

      numericString = "0x80C1";
      CallTryParse(numericString, NumberStyles.HexNumber);      
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      short number;
      bool result = Int16.TryParse(stringToConvert, styles, 
                                   CultureInfo.InvariantCulture, out number);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.", 
                           Convert.ToString(stringToConvert));
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version