rounding integers up...

1. Join Date
Dec 1969
Posts
34

## rounding integers up...

please excuse the rediculous question...but my brain just isn&#039;t functioning well today. In VBScript, can I take any number and round it to the next highest integer...ex. take 15.238 and round to 16 or 3.1 rounded up to 4?

2. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Hmmm

normally i sould have jsut said split and then check it the second element of the array is &#062; 0 but what if it already an integer.<BR><BR>Ok i guess forts do an instr and then see if the number is a...oh hell<BR><BR>If InStr(1, TheNumber, ".") &#062; 0 Then<BR> XanderIsADealBackouter = Split(TheNumber, ".")<BR> If CLng(XanderIsADealBackouter(1)) &#062; 0 Then<BR> TheNumber = CLng(TheNumber) + 1<BR> End If<BR>End If<BR><BR>

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

## Good question!

And don&#039;t fall for any hokey string based methods [grin!]. This is a MATH problem!<BR><BR>The obvious solutions:<BR><BR>&#060;%<BR>roundedUp = Int( theNumber + 0.9999999999 )<BR>%&#062;<BR>or<BR>&#060;%<BR>roundedUp = Round( theNumber + 0.4999999999 )<BR>%&#062;<BR><BR>But neither of those are perfect! If you had a number that was just a *very* tiny bit larger than an integer (e.g., 7.000000000001), then either of those might miss.<BR><BR>If you don&#039;t care, if getting it right to (say) 10 decimal places is close enough for you, go with either of those.<BR><BR>But if you want to be pedantic (and who do we know who is like that?):<BR><BR>&#060;%<BR>Function RoundUp( num )<BR>&nbsp; &nbsp; If num = Int( num ) Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; RoundUp = num<BR>&nbsp; &nbsp; Else<BR>&nbsp; &nbsp; &nbsp; &nbsp; RoundUp = Int( num ) + 1 <BR>&nbsp; &nbsp; End If<BR>End Function<BR>%&#062;<BR><BR>Eh? Make sense?<BR><BR>

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

## WAYYYY too complex...

Assuming that TheNumber *is* a number, then if you convert it to a string using the default VBS mechanisms, it will never have a decimal point unless there is something *after* the decimal point.<BR><BR>That is, CSTR(7.0) will always end up being "7".<BR><BR>So...<BR><BR>If InStr( CStr(theNumber), "." ) Then roundUp = Int(theNumber)+1 Else roundUp = Int(theNumber)<BR><BR>*BUT*<BR><BR>But any time you convert a number to a string, VBScript *will* do rounding FOR YOU!!! So doing something like CSTR(7.000000000001) will end up giving you "7"!!! Because VBS doesn&#039;t believe that you&#039;re a mathematician who cares about all those little trailing digits. <BR><BR>So... Don&#039;t ever get string conversions mixed into mathematical work. Something will almost surely go wrong go wrong go wrong go wro

5. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Well first i do NOT think

the number is going to ever be 7.000000000001<BR><BR>i used his examples and gave him a solution and that was a workable one :p<BR><BR>but yeah i liked your way VERY slightly better :)<BR><BR>

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

## LOL! Wow! Agreed 100%

How can *ANYBODY* top *THAT* message subject?<BR><BR>

7. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Hehehe

i saw that after i posted it...was hoping no one will notice that....{sigh} no such luck i see.<BR><BR>

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

## LOOK HERE! Computer math...

Akhilesh said:<BR><BR>&#062; ...I do NOT think the number is going to ever be 7.000000000001 <BR> <BR>But *that* is where you can get BADLY fooled! It *can* be a number like that, without you ever knowing it.<BR><BR>Try this simple little ASP page:<BR><BR>********** showComputerMath.asp **************<BR>&#060;HTML&#062;&#060;BODY&#062; <BR><BR>&#060;%<BR>sum = 0.0<BR>For count = 1 To 10<BR>&nbsp; &nbsp; sum = sum + 0.1<BR>Next<BR>amountOff = 1.0 - sum<BR>%&#062;<BR><BR>0.1 added to itself 10 times is &#060;% = sum %&#062;&#060;P&#062;<BR><BR>Which looks like 1.0...but the *actual* value is <BR>off by &#060;% = amountOff %&#062; !! &#060;P&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR>******** end of file *******

#### Posting Permissions

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