Integer Rounding

tehon3299

Centurion
Joined
Jan 6, 2003
Messages
155
Location
Liverpool, NY
How can I get an integer NOT to round? i.e. 1.88 I want to be 1 and not round up to 2. Or should it be a double? If so, I do I now show the decimal places?
 
In C# you just cast as an int, and it never rounds. Try:
C#:
decimal i = 1.88M;
Debug.WriteLine((int)i);

I don't know the equivalent in VB.NET, if you need it just ask.

-nerseus
 
I use this:

Visual Basic:
 intWhatEver  = Convert.ToInt32(Math.Floor(1.88))
 
You guys are mostly right - assuming you are using positive numbers. Floor behaves differently for negative numbers. If you do Math.Floor(-1.88) you'll get 2 (weird, but true). To do that properly using the math functions, you need to check for < 0 and use Math.Ceiling. Casting as (int) works in both cases.

-Ner
 
Back
Top