Double.CompareTo方法用于将当前实例与指定对象或Double对象进行比较。它将返回一个整数,该整数显示当前实例的值是大于,小于还是等于指定对象或Double对象的值。此方法的重载列表中有2种方法,如下所示:
- CompareTo(Double)方法
- CompareTo(Object)方法
Double.CompareTo(Double) Method
Double.CompareTo()方法用于将当前实例与指定的双精度浮点数进行比较,并返回一个整数,该整数显示此实例的值是小于,等于还是大于指定的double值。精度浮点数。
用法:
public int CompareTo (double value);
在这里,需要一个双精度浮点数进行比较。
返回值:此方法返回一个带符号的数字,指示该实例和值的相对值。
- 小于零:此实例小于value或该实例不是数字(NaN)并且value是数字。
- 零:此实例等于value,或者此实例和value都不是数字(NaN),PositiveInfinity或NegativeInfinity。
- 大于零:此实例大于value或此实例是一个数字,而value不是一个数字(NaN)。
下面的程序说明Double.CompareTo(Double)方法的用法:
示例1:
// C# program to demonstrate the
// Double.CompareTo(Double)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing value1
double value1 = 10d;
// Declaring and initializing value2
double value2 = 20d;
// compare both double value
// using CompareTo() method
int status = value1.CompareTo(value2);
// checking the status
if (status > 0)
Console.WriteLine("{0} is greater than {1}",
value1, value2);
else if (status < 0)
Console.WriteLine("{0} is less than {1}",
value1, value2);
else
Console.WriteLine("{0} is equal to {1}",
value1, value2);
}
}
输出:
10 is less than 20
示例2:
// C# program to demonstrate the
// Double.CompareTo(Double)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling get() method
get(5d, 7d);
get(5.5d, 4.5d);
get(10d, 20d);
get(7.5d, 19.5d);
}
// defining get() method
public static void get(double value1,
double value2)
{
// compare both double value
// using CompareTo() method
int status = value1.CompareTo(value2);
// checking the status
if (status > 0)
Console.WriteLine("{0} is greater than {1}",
value1, value2);
else if (status < 0)
Console.WriteLine("{0} is less than {1}",
value1, value2);
else
Console.WriteLine("{0} is equal to {1}",
value1, value2);
}
}
输出:
5 is less than 7 5.5 is greater than 4.5 10 is less than 20 7.5 is less than 19.5
Double.CompareTo(Object) Method
Double.CompareTo()方法用于将当前实例与指定对象进行比较,并返回一个整数,该整数指示当前实例的值是否小于,等于或大于指定对象的值。
用法:
public int CompareTo (object value);
在这里,它需要一个对象进行比较,或者为null。
返回值:此方法返回一个带符号的数字,指示该实例和值的相对值。
- 小于零:此实例小于value或该实例不是数字(NaN)并且value是数字。
- 零:此实例等于value,或者此实例和value都不是数字(NaN),PositiveInfinity或NegativeInfinity。
- 大于零:此实例大于value或此实例是一个数字,而value不是一个数字(NaN)。
异常:如果value不是Double,则抛出ArgumentException。
下面的程序说明Double.CompareTo(Object)方法的用法:
示例1:
// C# program to demonstrate the
// Double.CompareTo(object)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and initializing value1
double value1 = 10d;
// Declaring and initializing value2
object value2 = 20d;
// compare both double value
// using CompareTo() method
int status = value1.CompareTo(value2);
// checking the status
if (status > 0)
Console.WriteLine("{0} is greater than {1}",
value1, value2);
else if (status < 0)
Console.WriteLine("{0} is less than {1}",
value1, value2);
else
Console.WriteLine("{0} is equal to {1}",
value1, value2);
}
catch (ArgumentException e)
{
Console.WriteLine("value2 must be double");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
10 is less than 20
示例2:对于ArgumentException
// C# program to demonstrate the
// Double.CompareTo(object)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and initializing value1
double value1 = 10d;
// Declaring and initializing value2
object value2 = 1 / 3;
// compare both double value
// using CompareTo() method
int status = value1.CompareTo(value2);
// checking the status
if (status > 0)
Console.WriteLine("{0} is greater than {1}",
value1, value2);
else if (status < 0)
Console.WriteLine("{0} is less than {1}",
value1, value2);
else
Console.WriteLine("{0} is equal to {1}",
value1, value2);
}
catch (ArgumentException e)
{
Console.WriteLine("value2 must be double");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
value2 must be double Exception Thrown: System.ArgumentException
参考:
相关用法
- C# MathF.Tan()用法及代码示例
- C# MathF.Abs()用法及代码示例
- C# MathF.Log()用法及代码示例
- C# MathF.Max()用法及代码示例
- C# MathF.Min()用法及代码示例
- C# MathF.Sin()用法及代码示例
- C# MathF.Exp()用法及代码示例
- C# MathF.Cos()用法及代码示例
- C# MathF.Pow()用法及代码示例
- C# UInt16.GetHashCode用法及代码示例
- C# Single.IsFinite()用法及代码示例
- C# Single.CompareTo()用法及代码示例
- C# Single.IsNegative()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Double.CompareTo Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。