C# | Int16.ToString Method | Set – 2

Int16.ToString Method is used to convert the numeric value of the current instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows:

  1. ToString(IFormatProvider) Method
  2. ToString(String, IFormatProvider) Method
  3. ToString() Method
  4. ToString(String) Method

Here we will discuss the last two methods.



ToString() Method

This method is used to convert the numeric value of this instance to its equivalent string representation.

Syntax:

public override string ToString ();

Return Value: This method returns the string representation of the value of the current instance, consisting of a minus sign if the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Int16.ToString() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        Console.WriteLine("The String values are: ");
  
        // calling the method
        result(Int16.MaxValue);
        result(Int16.MinValue);
        result(5006);
        result(8459);
    }
  
    // result() method
    public static void result(short p)
    {
  
        // using ToString() method
        string str = p.ToString();
  
        // Display the value
        Console.WriteLine("The Corresponding String "+
                                "Value is: {0}", str);
    }
}

chevron_right


Output:

The String values are: 
The Corresponding String Value is: 32767
The Corresponding String Value is: -32768
The Corresponding String Value is: 5006
The Corresponding String Value is: 8459

ToString(String) Method

This method is used to convert the numeric value of this instance to its equivalent string representation, using the specified format.

Syntax:

public string ToString (string format);

Parameters: This method takes an numeric format string.

Return Value: This method returns the string representation of the value of this instance as specified by format.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Int16.ToString(String) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value
            short s1 = 123;
  
            // Declaring and initializing format
            string format = "E2";
  
            // using the method
            string result = s1.ToString(format);
  
            // Display the result
            Console.WriteLine("The value of string is {0}", result);
        }
  
        catch (FormatException e) {
            Console.WriteLine("Format is invalid.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The value of string is 1.23E+002

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.