[英]C# - How to find a difference between lowest and highest numbers
I have a List that contains multiple numbers like this:我有一个包含多个数字的列表,如下所示:
What I want to do, is to find a difference between lowest and highest number.我想要做的是找到最低和最高数字之间的差异。 In this case it would be 1.25 and 2.03 , which would make 0.78 .
在这种情况下,它将是1.25和2.03 ,这将使0.78 。
How should I do it?我该怎么做?
The steps are quite simple:步骤非常简单:
To implement this you can use LINQ:要实现这一点,您可以使用 LINQ:
// Intialize your list (or use the existing one)
var list = new List<decimal>{ 1.75m, 1.25m, 2.03m, 1.44m};
// The result is maximum of the list minus minimum of the list
var result = list.Max() - list.Min();
// Print or use the result
Console.WriteLine(result); // prints the result 0.78
首先确定最大值和最小值,然后从最大值中减去最小值。
There are a few different ways you can do it, but first a few questions:有几种不同的方法可以做到这一点,但首先要问几个问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.