C# BigInteger TryParse(String, NumberStyles, IFormatProvider, BigInteger)
Description
BigInteger TryParse(String, NumberStyles, IFormatProvider,
BigInteger)
Tries to convert the string representation of a number
in a specified style and culture-specific format to its BigInteger equivalent,
and returns a value that indicates whether the conversion succeeded.
Syntax
BigInteger.TryParse(String, NumberStyles, IFormatProvider, BigInteger)
has the following syntax.
public static bool TryParse(
string value,/*from w w w . java2 s. c o m*/
NumberStyles style,
IFormatProvider provider,
out BigInteger result
)
Parameters
BigInteger.TryParse(String, NumberStyles, IFormatProvider, BigInteger)
has the following parameters.
value
- The string representation of a number. The string is interpreted using the style specified by style.style
- A bitwise combination of enumeration values that indicates the style elements that can be present in value. A typical value to specify is NumberStyles.Integer.provider
- An object that supplies culture-specific formatting information about value.result
- When this method returns, contains the BigInteger equivalent to the number that is contained in value, or BigInteger.Zero if the conversion failed. The conversion fails if the value parameter is null or is not in a format that is compliant with style. This parameter is passed uninitialized.
Returns
BigInteger.TryParse(String, NumberStyles, IFormatProvider, BigInteger)
method returns true if the value parameter was converted successfully; otherwise, false.
Example
using System;//from w ww .j a va 2s. c o m
using System.Globalization;
using System.Numerics;
public class Example
{
public static void Main()
{
string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
"080", "0E293", "0F9A2FF", "0FFFFFFFF",
"0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
BigInteger number = BigInteger.Zero;
foreach (string hexString in hexStrings)
{
if (BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier,
null, out number))
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
else
Console.WriteLine("Cannot convert '{0}' to a BigInteger.", hexString);
}
}
}
public class BigIntegerFormatProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(NumberFormatInfo))
{
NumberFormatInfo numberFormat = new NumberFormatInfo();
numberFormat.NegativeSign = "~";
return numberFormat;
}
else
{
return null;
}
}
}
The code above generates the following result.