-3

Trying to define an Int32 variable here, but need it to return whole number, not decimals.

So I have this:

Int32 HalfCount = Convert.ToInt32(Math.Round(11 / 2, 0));

But get error.

Than I use this:

Int32 HalfCount = Math.Round(11/2);

Get another error. Can not convert Double to Decimal or something like that.

How am I supposed to declare an Int32 to be rounded in half? Or does Int32 automatically round it?

Could I just do this?

Int32 HalfCount = (11/2);

Doesn't look like a syntax error, but I have to know before I run the code, cause it will jack everything up if it is wrong.

I just need to return a whole number (without decimals), I don't care which way it goes (up or down is fine by me).

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
Solomon Closson
  • 6,111
  • 14
  • 73
  • 115

2 Answers2

1

Your last example will work, especially since you don't care which way the value is rounded.

Int32 HalfCount = (11/2);  // result is 5

This will drop the decimal portion of the result, storing only the integer portion in HalfCount.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

Int32 simply return unsigned integer which it does not return any decimal digits.