# RoundUp Function

Page 1 of 1

## 6 Replies - 30993 Views - Last Post: 29 April 2009 - 08:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=102319&amp;s=710d2693f624f41a5166879700667a83&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 damoUK

Reputation: 1
• Posts: 12
• Joined: 18-March 09

# RoundUp Function

Posted 29 April 2009 - 04:02 AM

I've been struggling with this for days, and scouring the web has returned nothing like what I need...

I need a function which will Round a number UP to the specified decimal precision, exactly the same as how Excel's RoundUp function works:
Roundup(dblVal, intDecPlaces)

So I need the following:
Roundup(0.896523, 4)
would return
0.8966

However, everything i've tried/found simply rounds to the nearest whole integer, or returns a normal rounded value, hence the above would return
0.8965

I've tried the following:
```Math.Round(dblval, intPrecision, midpointrounding.awayfromzero)
' and
Math.Round(dblval, intPrecision, midpointrounding.toeven)
```

and even custom functions, such as:

```	Public Function RoundUp(ByVal varValue As Object, _
ByVal iNum As Integer) As Double

'ignore the data types here, I was playing to see
'if changing any of the data types would swing
'the results (knowing full well they wouldnt make
'a difference, thats how fed up with this I am!)

Dim lNum As Long, xVal As Double, xVar As Object

xVar = Fix(varValue)
lNum = 10 ^ iNum

'get the rounded number result
xVal = xVar + CLng((varValue - xVar) * lNum) / lNum

Return xVal

End Function
```

I've even looked at converting this to a string, looking at the Xth decimal place, and incrementing it up by 1, then converting it back to a double, while this works (sort of) it seems to be a very roughshod way of doing it, and I'd rather do it mathematically than with lots of data conversion.

Does anyone have any ideas? This has been driving me crazy for days!!

Thanks

Is This A Good Question/Topic? 0

## Replies To: RoundUp Function

### #2 egof

Reputation: 20
• Posts: 132
• Joined: 27-March 09

## Re: RoundUp Function

Posted 29 April 2009 - 07:23 AM

Is the value always going to be 4 decimal places or does this change?
if its always 4 decimals then you could always add a const "00006" of 5 digits to your number then use the mathematically correct Math.Round to get your results.

Example
```double val = 0.896523;
double upBy = 0.00006;

val += upBy;//results 0.896573

val = Math.Round(val, 4);//results 0.8966
```

if the percesion has to change try this
```double upBy = 6.0;//constant

for (int i = 0; i < 5; i++)//5 is the percision plus one
{
upBy /= 10;
}
```

This post has been edited by egof: 29 April 2009 - 07:27 AM

### #3 egof

Reputation: 20
• Posts: 132
• Joined: 27-March 09

## Re: RoundUp Function

Posted 29 April 2009 - 07:40 AM

Oops sorry wrote it in c#. If this is a good idea I'll convert it to vb.net

### #4 Aurel300

Reputation: 13
• Posts: 230
• Joined: 10-November 07

## Re: RoundUp Function

Posted 29 April 2009 - 08:01 AM

No need to do such big functions.
This is the very function you need:
```Math.Round(number, precision)

```

Number - Double
Precision - Integer

Example:
```Math.Round(5.8907, 2)

```

Returns
5.89
...

This post has been edited by Aurel300: 29 April 2009 - 08:02 AM

### #5 egof

Reputation: 20
• Posts: 132
• Joined: 27-March 09

## Re: RoundUp Function

Posted 29 April 2009 - 08:14 AM

But for some reason damoUK wants this

Quote

So I need the following:
Roundup(0.896523, 4)
would return
0.8966

Math.Round(0.896523,4) would return 0.8965

### #6 damoUK

Reputation: 1
• Posts: 12
• Joined: 18-March 09

## Re: RoundUp Function

Posted 29 April 2009 - 08:17 AM

egof, thank you for your response.

As this is a VB.Net forum, I've converted your code to VB from C#, and modified it slightly to fit in with my function, and it seems to work perfectly.

For anyone else interested in the answer to this, I've used the following...

```	Public Function RoundUp(ByVal dblValue _
As Double, ByVal intDecPlaces As Integer) As Double

Dim upBy As Double = 6.0R 'constant
For i As Integer = 0 To intDecPlaces
upBy /= 10
Next
dblValue += upBy
Return Math.Round(dblValue, intDecPlaces)

End Function
```

If you're going to use this, it's probably a good idea to add some logic to check that you're not rounding up by the same number of, or more, decimal places than you've got in your original number.
For example, Roundup(0.8, 1) rounds up to 0.9, and Roundup(0.81,3) rounds to 0.811 which are probably not the answers you want, although is mathematically correct, based on the code used.

This post has been edited by damoUK: 29 April 2009 - 08:47 AM

### #7 damoUK

Reputation: 1
• Posts: 12
• Joined: 18-March 09

## Re: RoundUp Function

Posted 29 April 2009 - 08:44 AM

Aurel300, on 29 Apr, 2009 - 07:01 AM, said:

No need to do such big functions.
This is the very function you need:
```Math.Round(number, precision)

```

Number - Double
Precision - Integer

Example:
```Math.Round(5.8907, 2)

```

Returns
5.89
...

Thanks for your response, and it was a nice try, but I'm afraid that this is incorrect, the function I require (see the previous responses & my solution in VB.NET) give the correct answer.

The answer in your example, rounding Math.Round(5.8907,2) gives 5.89, as you rightly state, but I need this rounding UP to 5.90. Your answer rounds this DOWN (5.89 is less than 5.8907). If you don't believe me, try RoundUp(5.8907,2) in Excel.

As egof stated, I need it to RoundUp, the Reason for the roundup, is because I have some complex maths, which requires decimal precision at a minimum of 14 decimal places, this is needed mathematically, but I want to display 4,6 or 8 decimal precision to the user and displaying a number lower than the actual fraction is not an option.