Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top