# Thread: Help with Rounding up a decimal

1. ## Help with Rounding up a decimal

Using QTP 9.5

I need to roundup a decimal to the next whole number even if the decimal is less than .5.

This is what I have thanks to Google.

nMonths = DateDiff("M", PolicyEffDate, TransEffDate)
mMonths = Int(Round((nMonths/12)+.5))

It works great until I get to the whole numbers.

When the nMonths is 12 I need the mMonths to stay a 1 and not have a .5 added to it.

Example:
nMonths is 12
mMonths = 12/12 = 1 + .5 the Round up is a 2.
It should stay a 1.

Help!...Need a hint. I am stumped.

Thanks!

2. ## Re: Help with Rounding up a decimal

I believe the below code should resolve your problem. In this I am just checking if the nMonths/12 is returning a decimal value or an integer value.

nMonths = DateDiff("M", PolicyEffDate, TransEffDate)
mMonths=nMonths/12
mMonthsArr=split(mMonths,".")
If UBound(mMonthsArr) &gt; 0 Then
mMonths = Int(Round((nMonths/12)+.5))
End If
msgbox mMonths

Thanks,
Amey

3. ## Re: Help with Rounding up a decimal

<font class="small">Code:</font><hr /><pre>
roundup = 0
nMonths = DateDiff("M", PolicyEffDate, TransEffDate)
If mMonths Mod 12 &gt; 0 Then
roundup = 1
End if
mMonths = (nMonths\12)+roundup
</pre><hr />

Mod returns only the remainder(to determine if you would have received a decimal). \ performs an integer divide which ignores remainder. Nothing to do with qtp just basic...Basic [img]/images/graemlins/wink.gif[/img]

Since it appears you're just trying to determine the number of years and then round up if there's more than one month of a partial year you may as well start with a datediff on year anyway.
<font class="small">Code:</font><hr /><pre>
numYears = DateDiff("yyyy", PolicyEffDate, TransEffDate)
If DateDiff("M", PolicyEffDate, TransEffDate) mod 12 &gt; 0 then
numYears = numYears+1
End If
</pre><hr />

4. ## Re: Help with Rounding up a decimal

Thank you both so much....I have been fighting with this script for over two weeks now. Yesterday I had to figure out how to take 15 or 21 days from the transaction date. I hit this problem this morning and I did not have enough brain cells left to figure it out on my own.

I will give these solutions a try.

Thank you! Thank you!

5. ## Re: Help with Rounding up a decimal

I use the following tiny function call in my scripts since I generally need it to be true rounding and not Microsoft's version of it.

Function LRound(Result)
LRound = CLng(FormatNumber(Result, 0))
End Function

It's simple, quick and easy.

6. ## Re: Help with Rounding up a decimal

or you can just use Int function to do that trick:

examples:

Int(9.9) will give you 9
Int(9.1) also gives you 9

[img]/images/graemlins/cool.gif[/img]

7. ## Re: Help with Rounding up a decimal

He needed to round up(for any decimal value), not truncate.

#### Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.