本文整理匯總了C#中System.Numerics.BigInteger.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# BigInteger.Parse方法的具體用法?C# BigInteger.Parse怎麽用?C# BigInteger.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Numerics.BigInteger
的用法示例。
在下文中一共展示了BigInteger.Parse方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: switch
string stringToParse = String.Empty;
try
{
// Parse two strings.
string string1, string2;
string1 = "12347534159895123";
string2 = "987654321357159852";
stringToParse = string1;
BigInteger number1 = BigInteger.Parse(stringToParse);
Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number1);
stringToParse = string2;
BigInteger number2 = BigInteger.Parse(stringToParse);
Console.WriteLine("Converted '{0}' to {1:N0}.", stringToParse, number2);
// Perform arithmetic operations on the two numbers.
number1 *= 3;
number2 *= 2;
// Compare the numbers.
int result = BigInteger.Compare(number1, number2);
switch (result)
{
case -1:
Console.WriteLine("{0} is greater than {1}.", number2, number1);
break;
case 0:
Console.WriteLine("{0} is equal to {1}.", number1, number2);
break;
case 1:
Console.WriteLine("{0} is greater than {1}.", number1, number2);
break;
}
}
catch (FormatException)
{
Console.WriteLine("Unable to parse {0}.", stringToParse);
}
輸出:
Converted '12347534159895123' to 12,347,534,159,895,123. Converted '987654321357159852' to 987,654,321,357,159,852. 1975308642714319704 is greater than 37042602479685369.
示例2: catch
BigInteger number;
// Method should succeed (white space and sign allowed)
number = BigInteger.Parse(" -68054 ", NumberStyles.Integer);
Console.WriteLine(number);
// Method should succeed (string interpreted as hexadecimal)
number = BigInteger.Parse("68054", NumberStyles.AllowHexSpecifier);
Console.WriteLine(number);
// Method call should fail: sign not allowed
try
{
number = BigInteger.Parse(" -68054 ", NumberStyles.AllowLeadingWhite
| NumberStyles.AllowTrailingWhite);
Console.WriteLine(number);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
// Method call should fail: white space not allowed
try
{
number = BigInteger.Parse(" 68054 ", NumberStyles.AllowLeadingSign);
Console.WriteLine(number);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
//
輸出:
-68054 426068 Input string was not in a correct format. Input string was not in a correct format.
示例3: Main
//引入命名空間
using System;
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" };
foreach (string hexString in hexStrings)
{
BigInteger number = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier);
Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
}
}
}
輸出:
Converted 0x80 to -128. Converted 0xE293 to -7533. Converted 0xF9A2FF to -417025. Converted 0xFFFFFFFF to -1. Converted 0x080 to 128. Converted 0x0E293 to 58003. Converted 0x0F9A2FF to 16360191. Converted 0x0FFFFFFFF to 4294967295. Converted 0x0080 to 128. Converted 0x00E293 to 58003. Converted 0x00F9A2FF to 16360191. Converted 0x00FFFFFFFF to 4294967295.
示例4: GetFormat
public class BigIntegerFormatProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(NumberFormatInfo))
{
NumberFormatInfo numberFormat = new NumberFormatInfo();
numberFormat.NegativeSign = "~";
return numberFormat;
}
else
{
return null;
}
}
}
示例5: BigIntegerFormatProvider
BigInteger number = BigInteger.Parse("~6354129876", new BigIntegerFormatProvider());
// Display value using same formatting information
Console.WriteLine(number.ToString(new BigIntegerFormatProvider()));
// Display value using formatting of current culture
Console.WriteLine(number);
示例6: NumberFormatInfo
NumberFormatInfo fmt = new NumberFormatInfo();
fmt.NegativeSign = "~";
BigInteger number = BigInteger.Parse("~6354129876", fmt);
// Display value using same formatting information
Console.WriteLine(number.ToString(fmt));
// Display value using formatting of current culture
Console.WriteLine(number);
示例7: BigIntegerFormatProvider
// Call parse with default values of style and provider
Console.WriteLine(BigInteger.Parse(" -300 ",
NumberStyles.Integer, CultureInfo.CurrentCulture));
// Call parse with default values of style and provider supporting tilde as negative sign
Console.WriteLine(BigInteger.Parse(" ~300 ",
NumberStyles.Integer, new BigIntegerFormatProvider()));
// Call parse with only AllowLeadingWhite and AllowTrailingWhite
// Exception thrown because of presence of negative sign
try
{
Console.WriteLine(BigInteger.Parse(" ~300 ",
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
Console.WriteLine("{0}: \n {1}", e.GetType().Name, e.Message);
}
// Call parse with only AllowHexSpecifier
// Exception thrown because of presence of negative sign
try
{
Console.WriteLine(BigInteger.Parse("-3af", NumberStyles.AllowHexSpecifier,
new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
Console.WriteLine("{0}: \n {1}", e.GetType().Name, e.Message);
}
// Call parse with only NumberStyles.None
// Exception thrown because of presence of white space and sign
try
{
Console.WriteLine(BigInteger.Parse(" -300 ", NumberStyles.None,
new BigIntegerFormatProvider()));
}
catch (FormatException e)
{
Console.WriteLine("{0}: \n {1}", e.GetType().Name, e.Message);
}
// The example displays the followingoutput:
// -300
// -300
// FormatException:
// The value could not be parsed.
// FormatException:
// The value could not be parsed.
// FormatException:
// The value could not be parsed.