本文整理汇总了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);
}
}
输出:
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));
输出:
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);
}
}
示例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);
}
}
输出:
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)