how to round decimal number

CROw

New member
Joined
Jan 2, 2006
Messages
2
Programming Experience
Beginner
how can i extract round number from decimal number?

e.g. if i have a number like 2.668 and convert it to integer, i would get a number 3, and i need number 2...
 
There are all sorts of methods to convert decimal numbers to integers that behave in various ways. Decimal.Floor or Decimal.Truncate are what you want. Note that Floor rounds towards negative infinity while truncate rounds towards zero, e.g.

Decimal.Floor(2.668) = 2
Decimal.Floor(-2.668) = -3
Decimal.Truncate(2.668) = 2
Decimal.Truncate(-2.668) = -2
 
Note that both those methods return a Decimal object, so you would still need to convert it to an Integer object if that is the type you need. You can use CInt but I would use Convert.ToInt32.
 
Back
Top