我希望我有你想要的。
void Main()
{
var numbers =new [] {1, 2, 3, 5};
//ordering to make sure that ranges is ascending order
var orderedRanges = new []{1, 1.5, 2.6, 4, 50}.OrderBy(v => v);
var valuesInRanges = numbers.Select(number =>
FormatValueInRange(
number,
orderedRanges.Last(range => range <= number),
orderedRanges.First(range => range > number)));
var result = "Find " + string.Join("and ", valuesInRanges);
Console.WriteLine (result);
}
private string FormatValueInRange(double val, double lower, double upper)
{
return string.Format(@"{0} is {{{1}}} to {{{2}}} ", val, lower, upper);
}
打印:
Find 1 is {1} to {1.5} and 2 is {1.5} to {2.6} and 3 is {2.6} to {4} and 5 is {4} to {50}
[你嘗試過什麼?](http://mattgemmell.com/2008/12/08/what-have-you-tried/)這也可能是值得改寫你的問題,因爲它是一個小不清楚你想要做什麼。 –
我也想看看你到目前爲止已經嘗試過.. –
對不起,我很困惑。範圍2中的{1.5}和{2.6}如何? –