UInt64.ToString Method in C# with Examples | Set – 2
Last Updated :
01 May, 2019
UInt64.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 the current instance, consisting of a sequence of digits ranging from 0 to 9, without a sign or leading zeroes.
Example:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
Console.WriteLine( "The String values are: " );
result(UInt64.MaxValue);
result(UInt64.MinValue);
result(4946515);
result(8786786456);
}
public static void result( ulong p)
{
string str = p.ToString();
Console.WriteLine( "The Corresponding String "
+ "Value is: {0}" , str);
}
}
|
Output:
The String values are:
The Corresponding String Value is: 18446744073709551615
The Corresponding String Value is: 0
The Corresponding String Value is: 4946515
The Corresponding String Value is: 8786786456
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 the current instance as specified by format.
Exception: This method will give FormatException if the format parameter is invalid.
Example:
using System;
class GFG {
public static void Main()
{
try {
ulong val = 13244214;
string format = "E2" ;
string result = val.ToString(format);
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.32E+007
Reference:
Share your thoughts in the comments
Please Login to comment...