C# 比较两个 Double 类型数值的方法
在 C# 中,可以使用以下方法比较两个 double 类型数值:
- 使用比较运算符进行比较,例如:
double a = 1.234;
double b = 2.345;
if (a < b)
{
Console.WriteLine('a is less than b');
}
else if (a > b)
{
Console.WriteLine('a is greater than b');
}
else
{
Console.WriteLine('a is equal to b');
}
- 使用 Math 类中的静态方法进行比较,例如:
double a = 1.234;
double b = 2.345;
int result = Math.Compare(a, b);
if (result < 0)
{
Console.WriteLine('a is less than b');
}
else if (result > 0)
{
Console.WriteLine('a is greater than b');
}
else
{
Console.WriteLine('a is equal to b');
}
以上两种方法都可以用于比较两个 double 类型数值。
原文地址: https://www.cveoy.top/t/topic/qqHi 著作权归作者所有。请勿转载和采集!