Rounding Up

Discussion in 'ASP General' started by Brent Bortnick, Jul 2, 2003.

  1. Is there a way to make your numbers always round up. Ex:
    if the number is 0.0341 it will round to 0.035

    Brent
    Brent Bortnick, Jul 2, 2003
    #1
    1. Advertising

  2. If you always want it at the thousandths, you could say:

    <%
    function roundUp(n)
    if clng(n*10000) = 10 * clng(n*1000) then
    n = formatnumber(n, 3)
    else
    n = formatnumber(n + 0.0005, 3)
    end if
    roundup = n
    end function

    response.write "<br>" & roundUp(0.0341)
    response.write "<br>" & roundUp(0.03409)
    response.write "<br>" & roundUp(0.0340)
    response.write "<br>" & roundUp(0.0359)

    %>



    "Brent Bortnick" <> wrote in message
    news:47db01c340db$5ae0e710$...
    > Is there a way to make your numbers always round up. Ex:
    > if the number is 0.0341 it will round to 0.035
    >
    > Brent
    Aaron Bertrand - MVP, Jul 2, 2003
    #2
    1. Advertising

  3. "Brent Bortnick" wrote:
    >
    > Is there a way to make your numbers always round up. Ex:
    > if the number is 0.0341 it will round to 0.035


    JScript:
    val = Math.ceil(1000*val)/1000
    http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthceil.asp

    VBScript (depends on how you want to handle negative numbers):
    If val*1000 <> Fix(val*1000) Then val = Fix(val*1000+1)/1000
    If val*1000 <> Int(val*1000) Then val = Int(val*1000+1)/1000
    http://msdn.microsoft.com/library/en-us/script56/html/vsfctint.asp

    Of course, both methods introduce you to the problem of representing
    decimals with binary digits. See this for details:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q244699

    You can opt for string representations of those numbers with either of the
    following.

    JScript:
    http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthtofixed.asp

    VBScript:
    http://msdn.microsoft.com/library/en-us/script56/html/vsfctFormatNumber.asp


    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.
    Dave Anderson, Jul 2, 2003
    #3
  4. Brent Bortnick

    Evertjan. Guest

    Aaron Bertrand - MVP wrote on 02 jul 2003 in
    microsoft.public.inetserver.asp.general:

    > If you always want it at the thousandths, you could say:
    >
    > <%
    > function roundUp(n)
    > if clng(n*10000) = 10 * clng(n*1000) then
    > n = formatnumber(n, 3)
    > else
    > n = formatnumber(n + 0.0005, 3)
    > end if
    > roundup = n
    > end function
    >
    > response.write "<br>" & roundUp(0.0341)
    > response.write "<br>" & roundUp(0.03409)
    > response.write "<br>" & roundUp(0.0340)
    > response.write "<br>" & roundUp(0.0359)
    >
    > %>
    >


    Or:

    function roundUp(n)
    n = int(n*1000+0.999999)/1000
    roundup = formatnumber(n, 3)
    end function


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jul 2, 2003
    #4
  5. "Aaron Bertrand - MVP" wrote:
    >
    > if clng(n*10000) = 10 * clng(n*1000) then
    > n = formatnumber(n, 3)
    > else
    > n = formatnumber(n + 0.0005, 3)
    > end if


    CLng uses Banker's rounding, so it's wise to avoid in an ALWAYS up/down
    scenario. Compare the following with this function:

    roundUp(0.00105) --> 0.001
    roundUp(0.00106) --> 0.002

    http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q196652&ID=KB;EN-US;Q196652


    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.
    Dave Anderson, Jul 2, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    12
    Views:
    1,873
  2. valentin tihomirov

    rounding to integer

    valentin tihomirov, Feb 15, 2004, in forum: VHDL
    Replies:
    2
    Views:
    9,610
    Jonathan Bromley
    Feb 16, 2004
  3. C

    Rounding Numbers

    C, Aug 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    497
  4. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    504
    Karl Seguin
    Nov 22, 2005
  5. iksrazal
    Replies:
    1
    Views:
    731
    Chris Smith
    Jul 3, 2003
Loading...

Share This Page