c# - How to convert a decimal array to a double array

Convert a Decimal Array to a Double Array
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can convert a Decimal Array to a Double Array. But there is no direct method to convert a Decimal Array object to a Double Array instance. So, we have to perform some tasks to get a Double Array from Decimal Array elements.

The .net c# developers have to initialize an empty instance of a Double Array at the beginning. While initializing the Double Array instance, we will set the Double Array size exactly the same as the Decimal Array size. Next, we have to loop through the Decimal Array elements. While iterating the Decimal Array elements, we also set the Double Array’s elements value corresponding to the Decimal Array elements.

While setting the Double Array’s elements value .net developers have to convert the Decimal value to Double instance. In this .net example code, we will use the Convert class ToDouble() method to convert a Decimal instance to a Double instance. So finally, we will get a Double Array instance from the Decimal Array elements.

The Convert class converts a base data type to another base data type. The Convert class ToDouble() method converts a specified value to a double-precision floating-point number.

The Convert class ToDouble(Decimal) method overload converts the value of the specified decimal number to an equivalent double-precision floating-point number. The Convert class ToDouble (decimal value) method overload has a parameter named value.

The value parameter is the decimal number to convert. The Convert class ToDouble(Decimal) method returns a double-precision floating-point number that is equivalent to value.
convert-decimal-array-to-double-array.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        decimal[] decimalarray = new decimal[]
        {
            51.28M,
            12.93M,
            13.50M,
            29.84M,
            8.0M
        };

        Label1.Text = "decimal array.........<br />";
        foreach (decimal d in decimalarray)
        {
            Label1.Text += d.ToString() + "<br />";
        }
        
        double[] darray = new double[decimalarray.Length];
        for (int i = 0; i < decimalarray.Length;i++ )
        {
            darray[i] = Convert.ToDouble(decimalarray[i]);
        }

        Label1.Text += "<br />converted double array.........<br />";
        foreach (double d in darray)
        {
            Label1.Text += d.ToString() + "<br />";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - convert decimal array to double array</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - convert decimal array to double array
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="convert decimal array to double array"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples