Silly little Rounding Question

# Thread: Silly little Rounding Question

1. Senior Member
Join Date
Dec 1969
Posts
1,316

## Silly little Rounding Question

If I have the following expression:<BR><BR>NewValue = (Value1 - Value2) / 8<BR>I need to round NewValue to the next whole number. So, if it returns 2.12, I need to round it to 3<BR><BR>If the result is 19.98, it needs to round to 20.<BR>Is there an easy way to do this instead if using string functions to see if a decimal exists and act accordingly...

2. Senior Member
Join Date
Dec 1969
Posts
1,316

## Hack...welcome better suggestions

Public Shared Function RoundUp(ByVal NumericValue As Decimal) As Integer<BR> Dim strValue As String = NumericValue.ToString<BR> Dim strArray As String() = Split(strValue, ".")<BR> If strArray.GetUpperBound(0) = 0 Then Return CInt(strValue)<BR> If CInt(strArray(1)) &#062; 0 Then Return CInt(strArray(0)) + 1<BR> End Function<BR>

3. Senior Member
Join Date
Dec 1969
Posts
10,852

## RE: Hack...welcome better suggestions

Yep...Here&#039;s a better way.<BR>[code language="VB.Net"]<BR>NewValue = System.Math.Ceiling((Value1 - Value2) / 8)<BR>[/code]

4. Senior Member
Join Date
Dec 1969
Posts
10,852

## Note though....

That Ceiling gets you the nearest whole number furthest from zero on the number line. So, 2.12 will be 3, and 19.98 will be 20, but -2.12 will also be -3, and -19.98 will be -20. If this isn&#039;t the behaviour that you want for negative numbers, (I.E. you want -19 for -19.98) then you&#039;ll have to roll your own. I know it&#039;s been done several times here in the forums for VBS, and should be easy to convert to VB.Net.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not what the docs say!

They say that Math.Ceiling does "rounding towards positive infinity".<BR><BR>So Ceiling(-2.12) should be -2<BR><BR>etc.<BR><BR>Docs:<BR>http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemMathClassCeilingTopic.asp<BR><BR>QUOTE: <BR>Return Value<BR>The smallest whole number greater than or equal to [input argument]<BR><BR>Says nothing about the behavior you describe.<BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
10,852

## Huh. I may...

Have gotten it backwards. Sure looks like it, according to that. <BR><BR>Maybe it was the other behaviour that somebody wanted before? Oh well. I can&#039;t remember.

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Even if you needed to write your own...

WHY would you convert the number to a string??? One of the most inefficient things you can do (string to number is worse, though).<BR><BR>Of course, since you are using DECIMAL data types, everthing you are doing is horribly inefficient, anyway. (Decimal has to be done *in software*. Integer and float and double are all done in the *hardware*. Avoid Decimal unless you have a damned good reason to use it.)<BR><BR>So:<BR><BR>Public Shared Function RoundUp(ByVal dbl As Double) As Integer<BR> Dim id As Double = Int(dbl)<BR> If dbl &#060;&#062; id Then Return CInt(id)<BR> Return CInt(id) + 1<BR>End Function<BR><BR>Notice that the first line in the function uses INT and *NOT* CINT.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•