Rounder A Number Up!!

Discussion in 'ASP General' started by Adam Knight, Sep 14, 2003.

  1. Adam Knight

    Adam Knight Guest

    I am dividing a value by two..what i need is to round the number up if it is
    not a whole number.

    Any suggestions on how to do this ??

    Help appreciated!!

    AK
     
    Adam Knight, Sep 14, 2003
    #1
    1. Advertising

  2. Adam Knight

    Ken Schaefer Guest

    See if the number is not an integer, and if so, convert it to an integer and
    add one.

    Cheers
    Ken

    "Adam Knight" <> wrote in message
    news:%...
    : I am dividing a value by two..what i need is to round the number up if it
    is
    : not a whole number.
    :
    : Any suggestions on how to do this ??
    :
    : Help appreciated!!
    :
    : AK
    :
    :
     
    Ken Schaefer, Sep 14, 2003
    #2
    1. Advertising

  3. Cint(number+0.5)

    or

    Cint(number) + 1

    --

    ==========================================
    Hyper A.R.T.
    bart plessers
    Paul Van Ostaijenlaan 4
    3001 Heverlee
    BELGIUM
    tel: +32 (16) 23.40.85
    fax: +32 (16) 23.41.06
    ==========================================





    "Adam Knight" <> wrote in message
    news:#...
    > I am dividing a value by two..what i need is to round the number up if it

    is
    > not a whole number.
    >
    > Any suggestions on how to do this ??
    >
    > Help appreciated!!
    >
    > AK
    >
    >
     
    bart plessers, Sep 14, 2003
    #3
  4. Adam Knight

    ljb Guest

    sounds like a ceiling function

    http://www.vb2themax.com/Item.asp?PageID=CodeBank&Cat=110&ID=24

    vb2themax has the code

    Function Ceiling(Number)
    Ceiling = -Int(-Number)
    End Function


    "Adam Knight" <> wrote in message
    news:#...
    > I am dividing a value by two..what i need is to round the number up if it

    is
    > not a whole number.
    >
    > Any suggestions on how to do this ??
    >
    > Help appreciated!!
    >
    > AK
    >
    >
     
    ljb, Sep 15, 2003
    #4
  5. Actually, I just dealt with this...

    intScreens = (cnItems.RecordCount Mod 15) + Int(cnItems.RecordCount /
    15)

    MOD will return a zero if there is no remainder and a one if there is.
    Int forces the number to be a whole number, therefore getting ride of
    everything past the decimal.

    I'm sure you can figure out how to apply this to what you need to do.

    hth,
    Andrew

    DEVBuilder.org, http://www.DEVBuilder.org
    ASP,ASP.NET,VB.NET,PHP,Java,and SQL Support, all in one place.
     
    Andrew J Durstewitz, Sep 15, 2003
    #5
  6. Adam Knight

    Dan Brussee Guest

    On 15 Sep 2003 15:56:14 GMT, Andrew J Durstewitz
    <> wrote:

    >Actually, I just dealt with this...
    >
    >intScreens = (cnItems.RecordCount Mod 15) + Int(cnItems.RecordCount /
    >15)
    >
    >MOD will return a zero if there is no remainder and a one if there is.
    >Int forces the number to be a whole number, therefore getting ride of
    >everything past the decimal.
    >
    >I'm sure you can figure out how to apply this to what you need to do.
    >


    MOD does not return zero or one. The MOD operator returns the
    remainder after division. It can be a very helpful operator in certain
    cases.

    A typical case where I have used it is to cycle through a number of
    items, once per day. I take a date, do some date math to determine the
    number of days since that date till now, then use MOD to figure out
    which item to display...

    s(0) = "First Item"
    s(1) = "Second Item"
    s(2) = "Third Item"
    s(3) = "Fourth Item"
    s(4) = "Fifth Item"

    itm = s(datediff("d", "9/11/2001", now) mod 5)

    After dividing the number of days since 9/11, the MOD operator returns
    the remainder. Let's say there were only 2 days since 9/11. 2 MOD 5
    returns 2, so the s(2) would be returned. On the third day 3 MOD 5
    returns 3, so s(3) would be returned.

    Later, when it's 303 days after 9/11, 303 MOD 5 returns 3 (303 divided
    by 5 is 60 with a remainder of 3).
     
    Dan Brussee, Sep 15, 2003
    #6
  7. My bad, I mis-stated what I was trying to say. Your correct.

    However, if you use the MOD the poster will be able to round up.
    Basically if the MOD return is greater than 1 it's not a whole number.
    Turn it into an integer and add 1. Now it's the next higher number and
    whole.

    Andrew

    DEVBuilder.org, http://www.DEVBuilder.org
    ASP,ASP.NET,VB.NET,PHP,Java,and SQL Support, all in one place.
     
    Andrew J Durstewitz, Sep 15, 2003
    #7
  8. Adam Knight

    Dan Brussee Guest

    On 15 Sep 2003 17:26:15 GMT, Andrew J Durstewitz
    <> wrote:

    >My bad, I mis-stated what I was trying to say. Your correct.
    >
    >However, if you use the MOD the poster will be able to round up.
    >Basically if the MOD return is greater than 1 it's not a whole number.
    >Turn it into an integer and add 1. Now it's the next higher number and
    >whole.
    >


    Dont mean to harp on it, but how would MOD tell you if it's a whole
    number? It's really meant to be used with integers

    17 MOD 5 returns 2

    All numbers are whole.
     
    Dan Brussee, Sep 15, 2003
    #8
  9. "Adam Knight" wrote:
    >
    > I am dividing a value by two..what i need is to round the
    > number up if it is not a whole number.


    Whatever you do, avoid CInt. Here's why (see the section on Bankers
    Rounding)...
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q196652
    ....as well as the [Note] here:
    http://msdn.microsoft.com/library/en-us/script56/html/vsfctCInt.asp

    In short, here's a decent VBScript implementation of a "ceiling" function:

    Function RoundUp(val)
    RoundUp = Int(val) + Sgn(val - Int(val))
    End Function

    Just in case you care about negative numbers, consider what behavior you
    want for negative values before implementing this. "Rounding up" means
    that -1.2 rounds to -1. If you prefer that -1.2 round to -2, use something
    like this:

    Function RoundUp(val)
    RoundUp = Fix(val) + Sgn(val - Fix(val))
    End Function

    JScript is simpler, in a way. The first example above is merely:

    Math.ceil(val)

    The second is a bit trickier. Here are two implementations:

    1. val/Math.abs(val) * Math.ceil(Math.abs(val))
    2. val > 0 ? Math.ceil(val) : Math.floor(val)

    Enjoy exploring this.


    --
    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, Sep 15, 2003
    #9
  10. "Dan Brussee" wrote:
    >
    > Dont mean to harp on it, but how would MOD tell you if it's
    > a whole number? It's really meant to be used with integers


    "The modulus, or remainder, operator divides number1 by number2 (rounding
    floating-point numbers to integers) and returns only the remainder as
    result."
    http://msdn.microsoft.com/library/en-us/script56/html/vsoprmod.asp

    *** HOWEVER ***
    Mod uses Banker's Rounding, so it should be avoided at all costs:

    1.5 Mod 10 is 2
    2.5 Mod 10 is 2


    --
    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, Sep 15, 2003
    #10
    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. makok
    Replies:
    1
    Views:
    6,987
    Anders Hellerup Madsen
    Feb 23, 2004
  2. hari
    Replies:
    6
    Views:
    7,130
    hamilton
    May 2, 2004
  3. mayur
    Replies:
    2
    Views:
    1,052
    Natty Gur
    Jul 2, 2004
  4. Replies:
    5
    Views:
    560
    Pete Becker
    May 21, 2005
  5. Hako
    Replies:
    13
    Views:
    39,024
    Ron Adam
    Nov 10, 2005
Loading...

Share This Page