当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.Parse方法代码示例

本文整理汇总了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);
}
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:35,代码来源:BigInteger.Parse

输出:

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);
}    
//
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:29,代码来源:BigInteger.Parse

输出:

-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);
      }         
   }
}
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:19,代码来源:BigInteger.Parse

输出:

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;
      }      
   }
}
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:16,代码来源:BigInteger.Parse

示例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);
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:5,代码来源:BigInteger.Parse

示例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);
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:8,代码来源:BigInteger.Parse

示例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.
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:49,代码来源:BigInteger.Parse


注:本文中的System.Numerics.BigInteger.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。