Round a number UP ^

# Thread: Round a number UP ^

1. Sid Guest

## Round a number UP ^

hello all<BR><BR>Is it possible or has anyone been able to round a number up even if it is not mathematically correct ie...<BR>1.189673 to 2<BR><BR>Hope someone can help<BR><BR>Sid

2. Senior Member
Join Date
Dec 1969
Posts
760

## RE: Round a number UP ^

I don&#039t know if it helps, but JScript can do it.<BR><BR>http://msdn.microsoft.com/scripting/default.htm?/scripting/jscript/doc/jsmthceil.htm

3. ike Guest

## Use Int() and add 1 - but it's problematic

The VBScript Int() function rounds down - it returns the integer portion of a numeric value. You could use that then add 1.<BR><BR>e.g.<BR>s = 1.189673<BR>s = Int(s) + 1<BR><BR>The problem with this is that if you feed it an even number it&#039ll add 1 to that.

4. Richard A. Lowe Guest

## Int doesn't do banker's rounding

I just tried that and Int() doesn&#039t do banker&#039s rounding (rounds down on 4.5 rounds up on 5.5) that I can see.<BR><BR>So <BR><BR>Function UpRound(dblNum)<BR>&nbsp;&nbsp;&nbsp;UpRound = Int(dblNum)+1<BR>End Function<BR><BR>Will work.<BR>Richard

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

You misread his answer.<BR><BR>He did *not* expect Int to do rounding, at all. On the contrary.<BR><BR>He said to use Int--which chops ALL fractions off--and then to add one.<BR><BR>As he noted, the problem is that (for example) Int(4.00) is 4, so you don&#039t want to add 1 to that.<BR><BR>You *could* do <BR><BR>&#060;%<BR>n = Int( theValue )<BR>If n &#060;&#062; theValue Then n = n + 1<BR>%&#062;<BR><BR>But see my other answer...<BR><BR>

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

## Various ways...

&nbsp;<BR>(1) write your own function:<BR><BR>&#060;%<BR>Function RoundUp( val )<BR>&nbsp; &nbsp; Dim n<BR>&nbsp; &nbsp; n = Int( val )<BR>&nbsp; &nbsp; If n &#060;&#062; val Then n = n + 1<BR>&nbsp; &nbsp; RoundUp = n<BR>End Function<BR>%&#062;<BR><BR>7.3 &#062;&#062; 7 and then 7 &#060;&#062; 7.3, so 7 &#062;&#062; 8<BR>8.0 &#062;&#062; 8 and then 8 = 8.0 so 8 is left alone<BR><BR>****************<BR><BR>(2) Write it "inline":<BR><BR>n = Int(val) - ( val &#060;&#062; Int(val) )<BR><BR>That works because the subexpression<BR> &nbsp; &nbsp; &nbsp; val &#060;&#062; Int(val) <BR>evaluates to true or false and then true/false gets converted to -1/0. <BR><BR>But minus a -1 becomes +1, so it&#039s the same as the function code, really, but all written in nice linear coding.<BR><BR>

7. Richard A. Lowe Guest

## True!

In my mind<BR><BR>Even = 2, 4, 6, 8, 10<BR><BR>Not 4.00<BR><BR>But me answer was wrong anyway, due to the "even" number prob!<BR><BR>Thanks,<BR>Richard

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

## Heh! The code is easy; English is tough...

You&#039re right, of course. I was lucky to read the code first and *then* the English, so I realized what he meant by "even."<BR><BR>Incidentally, *if* you know that a number is dollars and cents, there is another way to do it:<BR><BR>&#060;%<BR>roundUp = Round( value + 0.495 )<BR>%&#062;<BR><BR>I think that&#039s right.<BR><BR>\$6.00 &#062;&#062; 6.495 rounds to 6<BR>\$6.01 &#062;&#062; 6.505 rounds to 7<BR>\$6.99 &#062;&#062; 7.485 rounds to 7<BR><BR>Ehh?<BR><BR>

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

## ALSO...see Code is easy...

See my reply with the subject "Code is easy; English is tough" to see another way. It only works if you *know* that the numbers you are dealing with represent money (dollars and cents, so always 2 digits after decimal), but it&#039s the shortest of all.<BR><BR>note: If you are working with a currency with 3 or 4 digits after the decimal point, it still works if you just adjust the value added before the rounding.<BR><BR>

10. Sid Guest

## Thanks To All

Thanks to all , Bill your first answer worked a treat.<BR>I Thank You<BR>Once again I Thank you<BR><BR>in the immortal words of Bobby Ball

#### Posting Permissions

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