UInt16.ToString Method in C# with Examples | Set – 2
UInt16.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:
- ToString(IFormatProvider) Method
- ToString(String, IFormatProvider) Method
- ToString() Method
- 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 this instance, which consists of a sequence of digits ranging from 0 to 9, without a sign or leading zeros.
Example:
// C# program to illustrate the // UInt16.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(UInt16.MaxValue); result(UInt16.MinValue); result(10); result(86); } // result() method public static void result(ushort p) { // using ToString() method string str = p.ToString(); // Display the value Console.WriteLine("The Corresponding String " + "Value is: {0}",str); } } |
The String values are: The Corresponding String Value is: 65535 The Corresponding String Value is: 0 The Corresponding String Value is: 10 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 numeric format string.
Return Value: This method returns the string representation of the value of the current instance as specified by format.
Exception: This method will give FormatException if the format parameter is invalid.
Example:
// C# program to demonstrate the // UInt16.ToString(String) Method using System; class GFG { // Main Method public static void Main() { try { // Declaring and initializing value ushort val = 84; // 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); } } } |
The value of string is 8.40E+001
Reference:
Recommended Posts:
- MathF.Sin() Method in C# with Examples
- MathF.Log() Method in C# with Examples
- MathF.Exp() Method in C# with Examples
- MathF.Abs() Method in C# with Examples
- MathF.Cos() Method in C# with Examples
- MathF.Tan() Method in C# with Examples
- MathF.Min() Method in C# with Examples
- MathF.Max() Method in C# with Examples
- MathF.Pow() Method in C# with Examples
- Type.FindMembers() Method in C# with Examples
- Int32.Equals Method in C# with Examples
- Int64.Equals Method in C# with Examples
- Int64.GetHashCode Method in C# with Examples
- Int16.Equals Method in C# with Examples
- Int32.CompareTo Method in C# with Examples
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.



