jQuery and ASP.NET

December 16, 2009

Convert a String Array to a Decimal Array using C# or VB.NET




The Array.ConvertAll() method is a very useful method to convert an array of one type to an array of another type. Here’s how to use ConvertAll() to convert a String[] to Decimal[]

C#

static void Main(string[] args)
{
string[] strArray = new string[] { "1164", "2213" };
decimal d;
decimal[] d1;
// Check if string can be converted to decimal equivalent
if (strArray.All(number => Decimal.TryParse(number, out d)))
{
d1 = Array.ConvertAll<string, decimal>(strArray, Convert.ToDecimal);
}
}

VB.NET

Sub Main(ByVal args() As String)
Dim strArray() As String = { "1164", "2213" }
Dim d As Decimal
Dim
d1() As Decimal
' Check if string can be converted to decimal equivalent
If strArray.All(Function(number) Decimal.TryParse(number, d)) Then
d1 = Array.ConvertAll(Of String, Decimal)(strArray, Convert.ToDecimal)
End If
End Sub

The first step is to use Enumerable.All(TSource) to determine whether all the elements in a sequence satisfy a condition, in our case, if all the elements of the array can be converted to its decimal equivalent. Once this can be done, we use ConvertAll() to convert an array of string to an array of decimal.



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


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

5 Responses to "Convert a String Array to a Decimal Array using C# or VB.NET"
  1. simi said...
    December 29, 2009 8:39 PM

    hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

    There is a good C# resource site, Have alook

    http://csharptalk.com

    simi

  2. Anonymous said...
    October 14, 2010 1:33 PM

    Great!!! thanks!!!

  3. kevininstructor said...
    August 2, 2011 2:09 PM

    The VB.NET version needs a slight alteration as follows using VS2008


    Dim SomeArray() As String = {"1164", "2.213", "29"}
    Dim decimals() As Decimal
    If SomeArray.All(Function(number) Decimal.TryParse(number, Nothing)) Then
    decimals = Array.ConvertAll(Of String, Decimal)(SomeArray, Function(c As String) CDec(c))
    Console.WriteLine("Success!!!")
    End If

  4. Suprotim Agarwal said...
    August 2, 2011 6:07 PM

    Thanks Kevin for the VB.NET code.

  5. kevininstructor said...
    August 3, 2011 6:07 AM

    Your welcome and thank you for sharing your expertise!!!

 

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