Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to literally define an array of decimals without multiple casting?

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 };
like image 798
Edward Tanguay Avatar asked Jul 06 '10 14:07

Edward Tanguay


People also ask

How do you fill an array without a loop?

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 );

What is C# decimal?

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.

How do you type an array?

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> .

What is array in simple language?

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).


1 Answers

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

like image 169
Asaph Avatar answered Sep 18 '22 15:09

Asaph