Open In App

SByte.ToString Method in C# with Examples | Set – 2

Improve
Improve
Like Article
Like
Save
Share
Report

SByte.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 the current 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:




// C# program to illustrate the
// SByte.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(SByte.MaxValue);
        result(SByte.MinValue);
        result(120);
        result(86);
    }
  
    // result() method
    public static void result(sbyte p)
    {
  
        // using ToString() method
        string str = p.ToString();
  
        // Display the value
        Console.WriteLine("The Corresponding String "
                              + "Value is: {0}",str);
                            
    }
}


Output:

The String values are: 
The Corresponding String Value is: 127
The Corresponding String Value is: -128
The Corresponding String Value is: 120
The Corresponding String Value is: 86

ToString(String) Method

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

Syntax:

public string ToString (string format);

Parameters: This method takes standard or custom numeric format string.

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

Example:




// C# program to demonstrate the
// SByte.ToString(String) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value
            sbyte val = 114;
  
            // Declaring and initializing format
            string format = "E2";
  
            // using the method
            string result = val.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);
        }
    }
}


Output:

The value of string is 1.14E+002

Reference:



Last Updated : 01 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads