-3
double one = tips[0];

double two = tips[1];
double three = tips[2];
double four = tips[3];
double five = tips[4];
double six = tips[5];
double seven = tips[6];

int average = one, two, three, four, five, six, seven;

The compiler is saying I need to convert a double to int but I don't know how. This is all I find on the internet.

public static double ToDouble(
    int value
)

But I don't understand it and don't know how to put it in my code to make it work.

gsharp
  • 27,557
  • 22
  • 88
  • 134

6 Answers6

1

You may cast it:

double d = 0;
int i = (int)d;

or convert it:

double d = 0;
int i = Convert.ToInt32(d);
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
1

You can use (int) to cast a double to an int, however the average should be double. For calculating the average, you should use LINQ:

double average = tips.Average();
//or
int average = (int) tips.Average();
King King
  • 61,710
  • 16
  • 105
  • 130
1

To actually compute your average, use LINQ! Namely Enumerable.Average(IEnumerable<double>)

double[] tips = new[] { 1.4, 2.6, 3.2 };

double average = tips.Average();
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0
Convert.ToInt32();

or

Int.parse();
Sasidharan
  • 3,676
  • 3
  • 19
  • 37
0

You could use the static Convert class.

The result would be:

var myInt = Convert.ToInt32(myDouble);
Jordy Langen
  • 3,591
  • 4
  • 23
  • 32
0

try:

int varInt = Convert.ToInt32(varDouble);

and also check this other answer: convert double to int

Community
  • 1
  • 1
Euclides Mulémbwè
  • 1,677
  • 11
  • 18