Generate Sequence of Float Numbers within a Range using LINQ




Sometime back, I had written a post to Generate Odd Numbers within a Range using LINQ. Here’s a small modification to use LINQ to generate float numbers within a Range say - 20 to 40.

C#

static void Main(string[] args)
{
var rng = Enumerable.Range(200, 200).Select(x => x / 10f);
foreach (float n in rng)
{
Console.WriteLine(n);
}
Console.ReadLine();
}

VB.NET (Converted Code)

Sub Main()
Dim rng = Enumerable.Range(200, 200).Select(Function(x) x / 10f)
For Each n As Single In rng
Console.WriteLine(n)
Next n
Console.ReadLine()
End Sub

As you know, the Enumerable.Range accepts two parameters: Start and Count. Since we are dividing the numbers in the range by 10F, the numbers gets generated in the following manner:

200/10 = 20 ; 201/10 = 20.1 ; 202/10 = 20.2

and so on..

OUTPUT

image



Will you give this article a +1 ? Thanks in advance




About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Generate Sequence of Float Numbers within a Range using LINQ"
 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions