Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    Thanked 0 Times in 0 Posts

    rounding numbers ASP/vbscript

    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 ). 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.
    does this sig match?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Use:

    Fix(Number)

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

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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:

    Code:
    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...

    Last edited by M@rco; 12-07-2003 at 12:21 AM.
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.
    does this sig match?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    True enough, but allida77 only wanted to drop the decimal places because he/she (???) was already adding one to the result.

    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"


  •  

    Posting Permissions

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