...

View Full Version : rounding numbers ASP/vbscript



allida77
12-06-2003, 05:47 PM
I have run into a problem that has existed for while but it has just now come to my attention. I have a page that returns a large amount of rows that I recently converted from using ado paging to the getRows() method. Well it sped up the page two fold and I thought that it was perfect (which is why I now have a problem :D). I have now realized a problem when I am trying to get the total number of pages. I get the total number of pages by the number of rows the client wants to see per page divided by the recordcount. So :

1000 (total records) / 100 (records per page) = 10 total pages

The problem lies when the division returns a remainder:

1000 (total records) / 30 (records per page) = 33.3 pages

So the logic is anytime the number of pages has a decimal you should +1. Easy enough but I have not found a simple way in vbscript to do this. So this is what I have:

'This tells me if there will be a remainder
If (iRecordCount Mod iRows) > 0 Then
iPageCount = CInt(iRecordCount / iRows) + 1
End If

Notice I use CInt() for the number and add 1. I thought by using CInt() it would just drop its decimal value. Well it does that and it also will round up. So if I have 33.7 doing a CInt(33.7) gives me 34 then I would add 1 creating to many pages. Is there a way to just drop the decimals on a number without rounding?

Currently my solution will be to check to see if the MOD is > .5. If so then I do not add one, if it is less then I add one. I am not sure why CInt() rounds in vbscript.

glenngv
12-06-2003, 06:06 PM
Use:

Fix(Number)

The decimal point and all digits to the right are chopped off.

M@rco
12-07-2003, 01:19 AM
That's not what allida77 wants.

Fix(4.33) = 4

allida77 wants an output of 5, for which there is no built in function in VBScript.

However, you can exploit the characteristics of the Int() function to get the desired output, like so:


Function RoundUp(Num)
RoundUp = -Int(-Num)
End Function

I suggest you both read up on the Int(), Fix(), and Round() functions in your VBScript documentation to understand the differences...

;)

allida77
12-07-2003, 06:52 PM
Thanks. :D

glenngv
12-08-2003, 01:58 AM
Originally posted by M@rco
That's not what allida77 wants.

Fix(4.33) = 4

allida77 wants an output of 5, for which there is no built in function in VBScript.

;)

Actually, allida77 had 2 conflicting statements.



So the logic is anytime the number of pages has a decimal you should +1.




Is there a way to just drop the decimals on a number without rounding?


In which I followed the latter. :o

M@rco
12-08-2003, 02:12 AM
True enough, but allida77 only wanted to drop the decimal places because he/she (???) was already adding one to the result.

:p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum