Rounding up problem vb.net
Ive been searching the forum for an easy way to round numbers but i couldnt find what i was looking for.
lets say if i have a variable and i want to round it up to the closest number so that for example 1.02 = 2, 3.6 = 4, 1.5=2 etc
is there any easy way of doing this without lots of code?
cant really get the round function to work the way i want, i must be doing something wrong and when i try to fix it - it justresults in more and more code. There must be a really easy way of doing this since it is a quite common thing you need?
when answering prtend your helping a 5 year old since im really new to programming and vs 2008.
Re: Rounding up problem vb.net
If you always want to round up to the nearest integer, then use something like this:
Code:
Dim d As Decimal = 1.02
Dim i As Integer = Math.Ceiling(d)
Re: Rounding up problem vb.net
Just note that Math.Ceiling rounds toward positive infinity, so positive numbers will round away from zero and negative numbers will round towards zero. If you're not using any negative numbers or that's what you want then there's no issue, but if you wanted all numbers to round away from zero then you'll have to jiggle it a bit, using Math.Floor for negative numbers.