How can I define an array of decimals without explicitly casting each one?
//decimal[] prices = { 39.99, 29.99, 29.99, 19.99, 49.99 }; //can't convert double to decimal
//var prices = { 39.99, 29.99, 29.99, 19.99, 49.99 }; //can't initialize...
decimal[] prices = { (decimal)39.99, (decimal)29.99, (decimal)29.99, (decimal)19.99, (decimal)49.99 };
Ooooh, use recursion! void to100(int[] array, int i, int v) { if( i < array. length ) { array[i] = v; to100( i+1, v+1 ); } } int[] array = new int[100]; too100( array, 0, 1 );
C# decimal precision The decimal type is a 128-bit floating point data type; it can have up to 28-29 significant digits. The following example compares the precision of the float , double , and the decimal types. Program.cs. float x = 1f / 3f; double y = 1d / 3d; decimal z = 1m / 3m; Console. WriteLine(x); Console.
To create an array type you can use Array<Type> type where Type is the type of elements in the array. For example, to create a type for an array of numbers you use Array<number> . You can put any type within Array<Type> .
An array is a collection of items of same data type stored at contiguous memory locations. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
Use the m
suffix.
decimal[] prices = { 39.99m, 29.99m, 19.99m, 49.99m };
Without the m
(or M
) suffix, the compiler treats it as a double.
-- http://msdn.microsoft.com/en-us/library/364x0z75(VS.71).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With