C# Int16 Parse(String, IFormatProvider)

Description

Int16 Parse(String, IFormatProvider) converts the string representation of a number in a specified culture-specific format to its 16-bit signed integer equivalent.

Syntax

Int16.Parse(String, IFormatProvider) has the following syntax.


public static short Parse(
  string s,
  IFormatProvider provider
)

Parameters

Int16.Parse(String, IFormatProvider) has the following parameters.

  • s - A string containing a number to convert.
  • provider - An IFormatProvider that supplies culture-specific formatting information about s.

Returns

Int16.Parse(String, IFormatProvider) method returns A 16-bit signed integer equivalent to the number specified in s.

Example

The following example parses string representations of Int16 values with the Int16.Parse(String, IFormatProvider) method.


using System.Globalization;
using System;//from www .  ja v  a  2s  . c  o  m
public class MainClass{
  public static void Main(String[] argv){  
    string stringToConvert;
    short number;
    
    stringToConvert = " 123 ";
    try
    {
       number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
       Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
    }
    catch (OverflowException)
    {
       Console.WriteLine("'{0'} is out of range of the Int16 data type.", 
                         stringToConvert);
    }
    
    stringToConvert = " + 123";                     
    try
    {
       number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
       Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
    }
    catch (OverflowException)
    {
       Console.WriteLine("'{0'} is out of range of the Int16 data type.", 
                         stringToConvert);
    }
    
    stringToConvert = " +123 "; 
    try
    {
       number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
       Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
    }
    catch (OverflowException)
    {
       Console.WriteLine("'{0'} is out of range of the Int16 data type.", 
                         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