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


C# BigInteger.CompareTo方法代码示例

本文整理汇总了C#中System.Numerics.BigInteger.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.CompareTo方法的具体用法?C# BigInteger.CompareTo怎么用?C# BigInteger.CompareTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Numerics.BigInteger的用法示例。


在下文中一共展示了BigInteger.CompareTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: foreach

object[] values = { BigInteger.Pow(Int64.MaxValue, 10), null, 
                    12.534, Int64.MaxValue, BigInteger.One };
BigInteger number = UInt64.MaxValue;

foreach (object value in values)
{
   try {
      Console.WriteLine("Comparing {0} with '{1}': {2}", number, value, 
                        number.CompareTo(value));
   }
   catch (ArgumentException) {
      Console.WriteLine("Unable to compare the {0} value {1} with a BigInteger.",
                        value.GetType().Name, value);
   }
}
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:15,代码来源:BigInteger.CompareTo

输出:

Comparing 18446744073709551615 with '4.4555084156466750133735972424E+189': -1
Comparing 18446744073709551615 with '': 1
Unable to compare the Double value 12.534 with a BigInteger.
Unable to compare the Int64 value 9223372036854775807 with a BigInteger.
Comparing 18446744073709551615 with '1': 1

示例2:

BigInteger bigIntValue = BigInteger.Parse("3221123045552");

byte byteValue = 16;
sbyte sbyteValue = -16;
short shortValue = 1233;      
ushort ushortValue = 1233;
int intValue = -12233;
uint uintValue = 12233;
long longValue = 12382222;
ulong ulongValue = 1238222;

Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, byteValue, 
                  bigIntValue.CompareTo(byteValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, sbyteValue, 
                  bigIntValue.CompareTo(sbyteValue)); 
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, shortValue, 
                  bigIntValue.CompareTo(shortValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, ushortValue, 
                  bigIntValue.CompareTo(ushortValue)); 
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, intValue, 
                  bigIntValue.CompareTo(intValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, uintValue, 
                  bigIntValue.CompareTo(uintValue)); 
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, longValue, 
                  bigIntValue.CompareTo(longValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, ulongValue, 
                  bigIntValue.CompareTo(ulongValue));
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:35,代码来源:BigInteger.CompareTo

输出:

Comparing 3221123045552 with 16: 1
Comparing 3221123045552 with -16: 1
Comparing 3221123045552 with 1233: 1
Comparing 3221123045552 with 1233: 1
Comparing 3221123045552 with -12233: 1
Comparing 3221123045552 with 12233: 1
Comparing 3221123045552 with 12382222: 1
Comparing 3221123045552 with 1238222: 1

示例3: StarInfo

//引入命名空间
using System;
using System.Collections.Generic;
using System.Numerics;

public struct StarInfo : IComparable<StarInfo>
{
   // Define constructors.
   public StarInfo(string name, double lightYears)
   {
      this.Name = name;
      
      // Calculate distance in miles from light years.
      this.Distance = (BigInteger) Math.Round(lightYears * 5.88e12);
   }

   public StarInfo(string name, BigInteger distance)
   {
      this.Name = name;
      this.Distance = distance;
   }

   // Define public fields.
   public string Name;
   public BigInteger Distance;

   // Display name of star and its distance in parentheses.
   public override string ToString()
   {
      return String.Format("{0,-10} ({1:N0})", this.Name, this.Distance);
   }

   // Compare StarInfo objects by their distance from Earth.
   public int CompareTo(StarInfo other)
   {
      return this.Distance.CompareTo(other.Distance);
   }
}
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:38,代码来源:BigInteger.CompareTo

示例4: Main

public class Example
{
   public static void Main()
   {
      StarInfo star;
      List<StarInfo> stars = new List<StarInfo>();

      star = new StarInfo("Sirius", 8.6d);
      stars.Add(star);
      star = new StarInfo("Rigel", 1400d);
      stars.Add(star);
      star = new StarInfo("Castor", 49d);
      stars.Add(star);
      star = new StarInfo("Antares", 520d);
      stars.Add(star);

      stars.Sort();

      foreach (StarInfo sortedStar in stars)
         Console.WriteLine(sortedStar);
   }
}
开发者ID:.NET开发者,项目名称:System.Numerics,代码行数:22,代码来源:BigInteger.CompareTo

输出:

Sirius     (50,568,000,000,000)
Castor     (288,120,000,000,000)
Antares    (3,057,600,000,000,000)
Rigel      (8,232,000,000,000,000)


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